javascript - Get id of an element through a function -
i have function called in onclick event in checkbox field.
<input type='checkbox' checked='' onclick='return changeenable();' id='someid'>
and function
function changeenable() { var val = $(this).attr('id'); alert(val); }
i have returns undefined
. syntax wrong or did miss something?
those checkboxes dynamically created , have different id's, that's why want id task.
note this
in changeenable
function window
. need pass reference element parameter function:
<input type='checkbox' checked='' onclick='return changeenable(this);' id='someid'>
function changeenable(el) { var val = el.id alert(val); }
or, improvement, use javascript attach events better separation of concerns:
<input type="checkbox" id="someid">
$(function() { $('#someid').change(function() { var val = this.id alert(val); } });
note above uses change
event of checkbox, better accessibility reasons.
Comments
Post a Comment