views:

318

answers:

2

Dropdown

In my site I have a set of SELECT dropdowns that are set up like this.

<OPTION class="addTitleTag">400</OPTION>

Search Box

There is also a search box that is like the following.

input type="text" class="textbox" onblur="search_SearchBlock_blur()" onfocus="search_SearchBlock_focus()" value="Search" name="g2_form[searchCriteria]" size="18" id="searchCriteria"

The Javascript

    jQuery(function() {
    jQuery('.addTitleTag').click(function() {
        titleText = jQuery(this).attr('text');
        jQuery("#searchCriteria").val(titleText);
        //$('#go_button').click(); //acts as if the search "go" button was clicked.
    });
});

The idea is when the Dropdown option is selected from the OPTION's, it takes the text of that option and copies it into the searchbox. The user then press's go on the searchbox to search for the text.This works fine in firefox. However, it does not fare well in Safari.I'm wondering what the issue is with it. I know that in a previous setup I did I used list tags (li) inside of an unordered list and safari seemed to be able to grab the text value fine. However, it does not grab the text from inside of the OPTION tag and I need to use the option tags in this case. I'm wondering if there is some sort of work around. Thank you!

+4  A: 

You'll want to get the select element's current value when the select's change event fires:

$(".myselect").change(function(){
  $(".search").val( $(this).val() );
});

Online Demo: http://jsbin.com/apuba/edit

Note that this method doesn't require adding onblur or onfocus event-logic in your HTML itself. Instead, this will bind on its own provided you give it adequate selectors.

<input type="text" name="search" class="search" />

<select class="myselect" name="foo">
  <option>100</option>
  <option>200</option>
  <option>300</option>
</select>

Nothing further is needed in the HTML.

Update

You can bind multiple dropdowns to do this too. Suppose you have the following:

<select id="product_1">
  <!-- options -->
</select>

<select id="product_2">
  <!-- options -->
</select>

You could bind up both of these with the following:

$("#product_1, #product_2").change(function(){
  $(".search").val( $(this).val() );
});

Note that I add new drop-downs merely by modifying the selector.

Jonathan Sampson
hello Jonathan, THANK YOU! I was making this much more complex than needed to. I appreciate the quick response. I had a question. What if I have 4 dropdowns on the same page. For example, one with an id of entry another with an id of horse etc.Would I just have to create 4 of the same js statement or is there a way to chain them together. Out of the 4 dropdowns, only one value will be searched for. So if they pick a value from entry it will populate the search box but if they then pick a value from horse it will overwrite what was in the search box with a new value. I hope i'm clear. :)
bob
@bob: I've updated my answer to address your question. Remember, if you found this answer to be helpful, consider accepting it by clicking the check-mark located beside it. Thanks!
Jonathan Sampson
Thanks Jonathan, this works great. I'm seeing that it does not work for IE6 and IE7. Could somebody out there test this as well to double check and possibly provide an explanation of why it is not working?
bob
@bob: I just tested in IE and it worked for me. I tested using jQuery 1.4.2. Be sure to update your local reference. If you continue to have problems, some have suggested binding to the `.click` event instead of `.change`, but I'm not sure I'd feel safe going that route.
Jonathan Sampson
Ah okay. Thanks for the feedback I'm thinking it may have been a false positive because I used "IETester" and I know sometimes it is not accurate with the javascript.
bob
A: 

In order to get the selected text of a ddl with jquery you need to do the following:

$("#yourdropdownid option:selected").text();

.val will only get you the selected value

This would be in the case that your Value and Text are different of course

This can be seen from the question jquery-get-selected-text-from-dropdownlist asked on StackOverflow

jmein