There's a div with a button. When you click the button a form appears. The div's id then gets assigned to a hidden field of the form. This works for the divs that are on the page when the page is loaded. It doesn't work for divs that are created by ajax after the page is loaded.

```
<div class=\"maincircle\">
    <div class=\"textcircle\" data-id=\"$name\" onclick=\"displayLevel3('$name');\">". $row['name'] ."</div>
    <button class=\"add1\" id=\"$name\" onclick=\"toggle_visibility('addleaf');\"></button>
    <button class=\"add2\" data-id=\"$name\" onclick=\"toggle_visibility('addsibling');\"></button>
    <button class = \"delete\" data-id= \"$name\"></button>
</div>

```

After clicking 'textcircle', ajax displays more divs with the same layout:

```
    $.ajax( { type : 'POST',
      data : {"node" : val},
      url  : 'displaylevel3.php',
      success: function ( data ) {
        $('#level3').html(data);
      },
      error: function ( xhr ) {
        alert( "error" );
      }
    });

}

```

The following javascript doesn't work for these div's buttons created by ajax:

```
var buttons = document.querySelectorAll('button');

for (var i=0; i<buttons.length; ++i) {
  buttons[i].addEventListener('click', clickFunc);
}

function clickFunc() {
  var id = this.id; 
  document.getElementById('parent').value = id;
  document.getElementById('sibling').value = id;
}

```

How to assign the value of a button created with ajax to a hidden form field?