html - Javascript get ID of a child element using information about the parent -
i have div created using javascript.
the div has varying number of child elements check boxes.
i want retrieve id's of checkboxes using loop.
the div has been passed through class using, , has been called:
oinput
i know working fine able retrieve number of child elements div has, using following line:
oinput.childnodes.length
but problem not know how retrieve id of child elements. want id's display in alert box now.
so far have:
for (var ii = 0; ii < oinput.childnodes.length; ii++) { var childid = oinput.childnodes[ii].id; alert("child " + childid); }
but not correct solution. have searched around there not seem clear answer question anywhere. looking solution using javascript display answer in alert box in loop.
this works me:
<html> <body> <div id="oinput"> <input id="1" type="text"> <input id="2" type="text"> <input id="3" type="text"> <input id="4" type="text"> <input id="5" type="text"> </div> <script type="text/javascript"> var oinput = document.getelementbyid('oinput'), ochild; for(i = 0; < oinput.childnodes.length; i++){ ochild = oinput.childnodes[i]; if(ochild.nodename == 'input'){ alert(ochild.id); } } </script> </body> </html>
if put 'script' tag in header didn't work because page hadn't loaded , div did not exist yet - perhaps problem?
tested in internet explorer 11 , chrome.
Comments
Post a Comment