views:

98

answers:

2

This is a very basic question. I am building a website using the google custom search api to display search results on my page. By default, google displays the search-results box on the page (without any actual results) at all times, and the list is populated when the user clicks the search button. My goal is to hide the search results box, and display it using jquery when a user clicks on the search button.

You can see a demo here but I set it up so when you click on the logo (top left), the results div shows. I cannot seem to figure out the correct selector for triggering the show on a click.I also need the div to show when "enter" is pressed on the search field. Here is my simple jquery:

$(document).ready(function(){
            alert($('input.gsc-search-button').length);
            $('input.gsc-search-button').click(function(){
            $('#cse').show(500);


         });

    });

I need to replace logo a with the appropriate selector that will show the cse div on click. You can see the seach input button in the top right of the page. Help!!!

+1  A: 

Use this selector:

$(function() {
  $('input.gsc-search-button').click(function(){ 
     $('#cse').show(500);
  });
});

Note: giving it an ID would be a better approach, but without changing any html, the above selector would work.

Update

Looks like google's is causing the trouble, clearing the event you're creating, put the hookup inside their handler so it works:

google.setOnLoadCallback(function(){
  var customSearchControl = new google.search.CustomSearchControl('012390824037940683019:gxvww9wrolu');
  customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
  var options = new google.search.DrawOptions();
  options.setSearchFormRoot('cse-search-form');
  customSearchControl.draw('cse', options);
  $('input.gsc-search-button').click(function(){ 
    $('#cse').show(500);
  });
}, true);
Nick Craver
that doesnt work for some reason!
JCHASE11
Did you wrap it in `$(document).ready()`? No?
BalusC
@JJCHASE11: What browser? It's working in chrome here...should always work. - Also do as Balus said, still needs document.ready around it...I'll update to make this clearer.
Nick Craver
@JCHASE11: Try `alert($('input.gsc-search-button').length);`, what's the result?
Nick Craver
Thanks Nick. Im trying in firefox and no luck. I also tried it in chrome without any luck. How would I use that code (js newbie)
JCHASE11
@JCHASE11: Just stick it inside the document ready: `<script type="text/javascript">$(function() {alert($('input.gsc-search-button').length); });</script>`
Nick Craver
I updated my script above. Is this correct? It doesnt fix it- it just gives me a javascript popup on page load that displays the value "0"
JCHASE11
im guessing that means the legnth of the div is 0, which is why the selector isn't working, because it has no actual area
JCHASE11
@JCHASE11: This is the same html as the page you linked? Doing any of this on the page you linked works in firefox and chrome here...are you testing against something else? You do what it to happen when you click the "Search" button up top, correct? (To answer your previous: It has a length of 0 because the selector matched no elements)
Nick Craver
Hmm that is strange that it is working for you. I am not having any luck over here and I am using the same files you are seeing. I do want it to happen when the search button is clicked, yes. I am uploading the new (exact) files I am making changes to with all the new changes and let me know if it works for you because its not for me. THANKS!
JCHASE11
if the alert told us that the selector matched no elements, doesnt that mean that selector won't work?
JCHASE11
@JCHASE11: Look at my answer again...it's the google handler causing the trouble, placing the click handler in there fixes it...before the google click handler was erasing the one you were making, placing it after they hook theirs up makes it work. Can you edit the google portion, or is that not a workable solution?
Nick Craver
this works perfectly. I also did the same on the"x" to remove the div. Thanks so much Nick!
JCHASE11
@JCHASE11: Welcome :)
Nick Craver
+1  A: 

In the past I have used Selector Detector and SelectorGadget to figure out complicated selectors. Not sure if this will solve your problem, but it is worth a shot.

I just wrote an article comparing the two that includes demos and download links. If you want to read more, check it out here: http://www.heinencreative.com/archives/articles/selector-detector-vs-selectorgadget/

Chris Heinen