jquery: get value of custom attribute in drop down

javascript, jquery

Solution

Inside the change handler `this` refers to the `select` element, but the `langid` is in the selected `option` element. So you need to find the selected `option` and then call `.attr("langid")` on that element

jQuery(function () {
    $(".translanguage").change(function () {
        var value = $(this).find('option:selected').attr("langid");
        alert(value);
    });
})

Demo: Fiddle

Problem

I have a simple JQuery code. ``` $(document).ready(function() { $( ".translanguage" ).change(function() { var value = $(this).attr("langid"); alert(value); }); }); ``` and i have html code. ``` <select name="translanguage" class="translanguage"> <option value="0">Please Select Language</option> <option class="transoption" value="1" langid="1">Urdu</option> <option class="transoption" value="2" langid="2">English</option> <option class="transoption" value="3" langid="3">Arabic</option> <option class="transoption" value="4" langid="4">Sindhi</option> </select> ``` When i run this program, it is giving undefined error in alert box. I need the value of langid as given in my option tage. So what is the problem in my code. ? Secondly what is the cause of undefined error in JQuery. Thanks

Original source