atul
atul

Reputation: 134

Adding placeholder to text box in google custom search engine in my html website

I have added google custom search engine using following code.

(function() {
    var cx = '005899633628958982661:wekn1lpckzg';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
                '//cse.google.com/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);
})();

Now I have a working search box for my site, the only problem is that I am unable to use placeholder for the search-text box.

I also tried the code

$('input.gsc-input').attr('placeholder', 'custom text here');

but it is not working.

Upvotes: 3

Views: 8400

Answers (2)

suketup
suketup

Reputation: 469

I used the following code, it worked for me!

(function() {   
    var cx = '!!!!!!!!!!!!!!!!!!!';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = 'https://cse.google.com/cse.js?cx='+ cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);
})();

window.onload = function(){
document.getElementById('gsc-i-id1').placeholder = 'SEARCH';
};

Upvotes: 7

ArnoldasM
ArnoldasM

Reputation: 206

This one works for me:

window.onload = demo;

function demo() {
    document.getElementById("gsc-i-id1").setAttribute("placeholder", "SEARCH")
}

Upvotes: 1

Related Questions