I was asked to help add an onChange to a select so I came up with this:
$("select[name=component]").change(function() {
$("input[name=Code]").val(JSONObject[$(this).selectedIndex].code;
$("input[name=Category]").val(JSONObject[$(this).selectedIndex].category;
$("input[name=UOM]").val(JSONObject[$(this).selectedIndex].uom;
});
The asker told me it didn’t work until he changed $(this) to this
That only makes sense to me, if jQuery does not use the name selectedIndex of what I assume is a jQuery object.
If not, would I just need to use .attr("selectedIndex")?
Is jQuery converting the object on the fly from $(this) (jQuery object) to a form element or select object?
The jQuery object is not really an extension of the DOM element: The DOM element becomes a child of the jQuery one.
It is in the
0member of the jQuery object, so if for some reason one needs to use the jQuery object (which as @nickf in the context of this example is insane – you would just usethis.selectedIndexinstead), one would have to useto access the original attribute.