How can I use Javascript to access sub-elements?

Posted by Steve on Stack Overflow See other posts from Stack Overflow or by Steve
Published on 2010-12-31T17:48:32Z Indexed on 2010/12/31 17:53 UTC
Read the original article Hit count: 107

Filed under:
|
|
|

I am dynamically creating forms dynamically according to a users actions. It is an entry-box and a two "buttons" for each instance. Each instance will be wrapped in a unique div tag.

What I tried to do without success is when I dynamically create the "button" I attach a function with the input variable containing the div of its instance. This a brief excerpt:

  var newDivClass = document.getElementById("instance"+1);
  button1.innerHTML = "<a href=\"#\" onclick=\"buttons("+newDivClass+");\" id=\"button1\"> Button1 </a>";

  function buttons(selected) {
        //I want this to select the first instance 
        //of button1 found within div newDivClass
        selected.getElementById("button1");
        //I also tried
        //this.getElementById("button1");
        //selected.getChildren[0]; 
  }

The problem appears to be in passing newDivClass to the the actual function.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html