How to retrieve the reference to an html object the fired an event in JavaScript? -
i pretty new in javascript , have following problem. jsp page have this:
<tbody> <c:foreach items="${listaprogetti.lista}" var="progetto" varstatus="itemprogetto"> <tr id='<c:out value="${progetto.prgpro}" />'> <td><input type="checkbox" class="checkbox" onchange="changecheckbox()"></td> ..................................................... ..................................................... ..................................................... </tr> </c:foreach> </tbody>
so can see clicking on checkbox call changecheckbox() simple:
function changecheckbox() { alert("into changecheckbox()"); var checkedrowlist = new array(); }
the problem function have retrieve input object fire change event (i think this)
how can obtain reference clicked checkbox in previous changecheckbox() function?
the element available in event handler content attribute, not in changecheckbox
. can pass argument.
using
this
changecheckbox(this)
function changecheckbox(elem) { elem.style.marginleft = '100px'; }
<input type="checkbox" onchange="changecheckbox(this)" />
using
event
:changecheckbox(event.target)
function changecheckbox(elem) { elem.style.marginleft = '100px'; }
<input type="checkbox" onchange="changecheckbox(event.target)" />
note better use addeventlistener
:
document.queryselector('input').addeventlistener('change', changecheckbox);
then function changecheckbox
receive event object first parameter, , current target element this
value.
document.queryselector('input').addeventlistener('change', changecheckbox); function changecheckbox(e) { e.target.style.marginleft = '100px'; }
<input type="checkbox" />
Comments
Post a Comment