How to change a parameter value using jQuery?

I am looking for a way to change a parameter value from a select tag when users click on a link.

For example, I have an html selection option:

<select> <option value="0">Please Select</option> 
<option value="1">red</option> 
<option value="2">white</option> 
</select>

And I have 2 links. <a href="#" title="red" class="preview_link">red</a> <a href="#" title="white">white</a> When the user clicks red, the option switches to red, and white switches to white. I am using the following code, but it does not work.

 jQuery("a.preview_link").click(function() {
    var title = jQuery(this).attr("title");
        jQuery(this).parent('p').children("select :selected").text(title);
 });

Any suggestions?

+3
source share
3 answers

Your path will set the option 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.

+5
source

It?

$("select #some-option").attr("selected", "selected");

Or that?

$("select").val(index);
+1
source

, : , , <option/> , :

$("select option").each(function() { 
    if($(this).text() == "red")) {
        this.selected = true; 
    }
});

: jQuery select(). , DOM.

0
source

Source: https://habr.com/ru/post/1755060/


All Articles