How To Change Option Value With Jquery?
I am looking for a way to change the option value from a select tag when users click on a link. For example I have a select option html:
Solution 1:
Your way will set the option's text to the title attribute. Try:
jQuery("a.preview_link").click(function() {
var title = jQuery(this).attr("title");
jQuery(this).parent('p').find("select option").filter(function() {
return $(this).text() == title;
}).attr('selected', 'selected');
});
See filter
.
Solution 2:
Hmm, you're going about this wrong: Instead of changing the text of the selected option you need to loop over the <option/>
elements and select the appropriate one, something like this:
$("select option").each(function() {
if($(this).text() == "red")) {
this.selected = true;
}
});
Edit: There might be a select()
function in jQuery. If that's the case then use that over the DOM property.
Solution 3:
This?
$("select #some-option").attr("selected", "selected");
Or this?
$("select").val(index);
Post a Comment for "How To Change Option Value With Jquery?"