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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -