add search button on search box on Custom Search Engine

970 views Asked by At

How to add search button to HTML? I have tried adding it, but when I search example YouTube that shows the resultant but don't show my search text YouTube how I can fix this, what I need to change here in this code? Thanks

.main{
 position: absolute;
 top: 10px;
    left: 25%;
 width: 50%;
 height: auto; 
 text-align: center;
}
.q{
 padding: 5px;
 width: 90%;
 margin-top: -30px;
 border: solid 1px #c68e00;
 font-size: 17px;
 font-family: Arial;
 height: 40px;
}
<script>
    (function() {
        var cx = '004533415228465222070:r2ak9s12hwi';
        var gcse = document.createElement('script');
        gcse.type = 'text/javascript';
        gcse.async = true;
        gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx;
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(gcse, s);
    })();
</script>
<gcse:searchresults-only></gcse:searchresults-only>
</div>

<div class="main">
 <br/><br/>
 <form action="" method="GET">
 <input class="q" name="q" placeholder="Search by Name or type URL" title="Search ..." value="" /> 
</form>

1

There are 1 answers

1
Rohit.007 On BEST ANSWER

I am not sure about the result but hope this work.

Added id="searchbox" in search input. and these two lines.

var urlParams = new URLSearchParams(window.location.search);
document.getElementById('searchbox').value = urlParams.get('q');

Your complete code now looks like.

<div class="main">
    <br/><br/>
    <form action="" method="GET">
 <input class="q" name="q" id="searchbox" placeholder="Search by Name or type URL" title="Search ..." value="" /> 
</form>
</div>
<script>
    (function() {
        var cx = '004533415228465222070:r2ak9s12hwi';
        var gcse = document.createElement('script');
        gcse.type = 'text/javascript';
        gcse.async = true;
        gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx;
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(gcse, s);
    })();
var urlParams = new URLSearchParams(window.location.search);
document.getElementById('searchbox').value = urlParams.get('q');
</script>
<gcse:searchresults-only></gcse:searchresults-only>