Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data attribute in jquery

Tags:

html

jquery

Here it's my html code

<select class="data">
    <option value="This is my test code" data-id='123'/>
</select>

Now I want to access a data attribute in jquery here it's my jquery code

$( ".data" ).change(function() {
    var Id = $(this).data("id")
    alert("id:  "+Id);
});

but when I'm tried this one it's give a undefine value. please help me.

like image 748
Rahul Patil Avatar asked Sep 29 '26 18:09

Rahul Patil


1 Answers

Use this

$( ".data" ).change(function() {
    var Id =  $(this).find('option:selected').data("id")
    alert("id:  "+Id);
});
like image 52
I'm Geeker Avatar answered Oct 02 '26 14:10

I'm Geeker