Add a default search text(item) in the search box

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Add a default search text(item) in the search box

This topic contains 4 replies, has 2 voices, and was last updated by

 
Keymaster
6 years, 6 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #5305

    Participant

    Hi,

    I want to add a default search text in the search box.so that when I click the search button it should search that item .Which PHP file I need to edit for this purpose and how should I apply the code?Any hint please?

    Earnest, I know this question is a bit typical because the search boxes are meant for general search and not a fixed item search but I want for some purpose.So I would appreciate if you reply to this question.

    #5306

    Hi!

    There are several ways to do that. I would not recommend editing the php code, as an update will ruin your changes.

    I would rather recommend a javascript solution, which you can add to your site footer between script tags:

    
    jQuery(function($) {
        var text = 'default text';
    
        $('input.orig').val(text);
    });
    

    Just change the text variable to whatever you need to 🙂

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #5309

    Participant

    Hi Ernest,

    Thanks for your reply again. Yes, you are correct if I use PHP to edit I won’t be able to use the search box instances for the general purpose for which it is meant.

    Now for your suggestion to apply javascript and the jquery which you mentioned, can you please tell me how will this be connected to your search box.How can I write the code to get an instance of search box on the page with the same panel and use the search settings from there with this jquery mentioned above?I don’t know anything about jquery but if you explain a bit I can try to apply.Your reply would be highly appreciated.

    #5361

    Participant

    What is ‘input.orig’ in the above jquery function you have suggested?

    Suppose the name of my instance for search box is ‘search1’ , how can I connect the above Jquery function to the instance ‘search1’ ?

    Earnest, I would really appreciate if you could give just 2-3 mins further to help me go ahead in this matter.

    #5365

    Hi!

    I’m assuming you use multiple instances. Every search bar instance has a unique ID. The ID format is like this:

    ajaxsearchpro{instance_ID}_{occurence_number}

    As you can see there are 2 numbers, one identifiing the instance and the other tells how many times this search instance is placed on the same page.
    So for example if the first instance is used once, then it’s ID is going to be this:

    ajaxsearchpro1_1

    If you use this instance again on the same page (the same shortcode twice), then it’s going to be:

    ajaxsearchpro1_2

    Same is true for the results and the settings container as well:

    ajaxsearchprosettings1_1

    and

    ajaxsearchprores1_1

    Now, if you want to target every occurence of the first instance, you can use this:

    
    jQuery(function($) {
        var text = 'default text';
    
        $("div[id*='ajaxsearchpro1_'] input.orig").val(text);
    });
    

    To target only the first occurence of the first instance:

    
    jQuery(function($) {
        var text = 'default text';
    
        $("#ajaxsearchpro1_1 input.orig").val(text);
    });
    

    Same way you can target the settings container:

    $(“div[id*=’ajaxsearchprosettings1_’] “) ….

    or only the first occurence:

    $(“#ajaxsearchprosettings1_1”) ….

    Please note that these are advanced uses of jQuery and CSS. To understand how all this works you should definitely google “jQuery basics” and “CSS selectors” for more detailed explanation.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.