There doesn't seem to be a .childNode function built into javascript.  So, what is the easiest way to access child elements?  If I have a <div>, and I want to access a child <img>, is there a simple way to do this?  

(I don't want to access it directly, via getElementById - I'm only asking if there is some way to access childNode via the parentNode.)

Who is Participating?
KennyTMConnect With a Mentor Commented:
Hi. Use ".childNodes[i]" (note the "s": there maybe more than 1 children in a node), ".firstChild" or ".lastChild".
Pravin AsarConnect With a Mentor Principal Systems EngineerCommented:
here is a code which picks only images from div

<script language="javascript">
function getImages (obj) {
      var imgs = new Array();
      var iCnt=0;
      if (!obj) { return; }
      for (var ix=0; ix < obj.childNodes.length; ix++) {
            var cObj = obj.childNodes[ix];
            if (!cObj.tagName) { continue; }
            if (cObj.tagName.toLowerCase().match('img')) {
                  imgs[iCnt++] = cObj;
      return imgs;
<input type="button" value="Change Style" onClick="SwitchStyle()">
<div class="one" onClick="var imgs=getImages(this); alert (imgs.length);">
<h3>The Click on the button to switch class</h3>
<img name="1" src="../images/0.gif" width="100" height="100">
<p>This is a text</p>
All Courses

From novice to tech pro — start learning today.