1

I have a content search web part that is optional to use. If a user finds an item through the filtered navigation I don't want them to see the content search web part. If they use the search box I want the web part to show the appropriate search results. I can get the web part to get the correct results from the correct source, but now it defaults to showing the items. Is there a way I can make the default show no items and then still run the correct search when a value is used in the search box?

2

One option that comes to my mind is using jQuery. On page load check if the search box has any values, based on that Show/Hide the results. Something like

jQuery(document).ready(function(){
    var searchValue = jQuery('#searchboxid').val();
    if(searchValue == null || searchValue == '' || searchValue == 'Search...') {
        jQuery('#webpartid').hide();
    }
});

You can find the searchboxid and webpartid using browser developer tools.

Edit

Handling key events in Search Box

jQuery('#ctl00_PlaceHolderSearchArea_SmallSearchInputBox1_csr_sbox').keyup(function(e){
    var code = e.which;
    if(code==32||code==13||code==188||code==186) {
        //If required you can check if the box is empty
        //Handle backspace etc.
        jQuery('#webpartid').show();
    }
});
  • Tried this, there isn't a refresh or page navigation when you search for a term so the web part stays hidden. – Jordan Nov 25 '14 at 19:52
  • @Jordan in that case on page load you should hide the webpart using jQuery and then hook an onchange event handler on the search box and then show it. – Amal Hashim Nov 25 '14 at 19:56
  • I put an onchange event on my '#searchboxid' and it doesn't trigger my function when I search. – Jordan Nov 25 '14 at 20:09
  • @Jordan I have updated my answer. I tried it on my dev site and its working as expected. You should change the #id – Amal Hashim Nov 25 '14 at 20:13
  • This does work. Is there a way to change it on submit that you know of? Somehow to check when the .val() changes but obviously not an onchange handler as that didn't work. – Jordan Nov 25 '14 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.