javascript - jquery $(this).attr(…) returns undefined -
javascript - jquery $(this).attr(…) returns undefined -
i'm trying title of option element, keeps returning undefined. happens $(this).attr("name")…and $(this).attr("value"), curiously $(this).val() works (as expected). yet, i'm able set value $(this).attr("value", "baz").
fiddle: http://jsfiddle.net/jshado1/jgajc/1/
this points <select> element. selected option, use:
this.options[this.selectedindex] full code (you can safely disclose $opt's jquery wrapper, , utilize $opt.title , $opt.name, these safe across browsers):
$('select').change(function() { var $opt = $(this.options[this.selectedindex]), t = $opt.attr("title"), n = $opt.attr("name"), v = this.value; $("#name").text("name: "+n); $("#title").text("title: "+n); $("#value").text("value: "+v); }); another method, jquery-way is:
$('select').change(function() { var $opt = $(this).children(':selected'), t = $opt.attr("title"), n = $opt.attr("name"), v = this.value; $("#name").text("name: "+n); $("#title").text("title: "+n); $("#value").text("value: "+v); }); javascript jquery find switch-statement
Comments
Post a Comment