简体   繁体   中英

How do I get the ID text of the checked item in the select element?

How do I get the ID text of the checked item in the select element?

I need it to change the color of a text, so for example one id could be "red".

<script>
function add(){
    var str = document.getElementById("item");
    str.style.color = ;
}
</script>

Basically I named the id's in the option's as "red", "yellow" and "blue" so I need something to get those id's as text and add it after str.style.color = .

I have tried using jquery.

In this "my_select" is the selector in this. you can get that full element by :selected

$("#my_select").change(function() {
var id = $(this).children(":selected").attr("id");
});

you can get full element by this keyword. try to by log

$("#my_select").change(function() {
console.log(this);
});

after getting full element u can do anything with it like changing color or bold etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM