javascript - How to assign a value of an element created with ajax to a hidden form field? -
there's div button. when click button form appears. div's id gets assigned hidden field of form. works divs on page when page loaded. doesn't work divs created ajax after page 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 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 these div's buttons created ajax:
var buttons = document.queryselectorall('button'); (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 assign value of button created ajax hidden form field?
you have use jquery event-delegation:-
$(document).on('click','button',function(){ var id = $(this).attr('id'); $(this).closest('#parent').val(id); $(this).closest('#sibling').val(id); });
Comments
Post a Comment