javascript - this.parentNode returns undefined when run using Firefox -
when following code run via firefox , click x button, javascript returns tmpfield undefined. need parentnode of button div.
<head> <title></title> <script type ="text/javascript"> function removeitem() { if (document.addeventlistener) { var container = document.getelementbyid("inputcontainer"); var tmpfield = this.parentnode; alert("field: " + tmpfield); } } </script> </head> <body> <table width="100%"> <tr> <td style="width: 10%; vertical-align:top;">field:</td> <td id="inputcontainer"> <div id="divinput1" style="display: inline;"> <input id="txtcomment1" name="txtcomment" type="text" maxlength="35" /> <input id="btndelete1" value="x" type="button" name="btndelete" onclick="removeitem()" /> </div> </td> </tr> </table> </body> </html>
in scenario, dynamically create multiple divs within "inputcontainer". each of divs has same structure containing text box , delete button (with ids containing next increment). important me able return parentnode of button div. have code work in ie not firefox. ideas how resolve?
thanks...
use html:
<input id="btndelete1" value="x" type="button" name="btndelete" onclick="removeitem(this)" />
and js:
function removeitem(button) { var container = document.getelementbyid("inputcontainer"); var tmpfield = button.parentnode; alert("field: " + tmpfield); }
demo: http://jsfiddle.net/jwpds/1/
the context of removeitem
function call global, this
refers window
. specific element clicked, need pass this
in function call, , access parameter in function.
and have no idea why checking document.addeventlistener
, can assure you, don't need doing.
Comments
Post a Comment