javascript - Naming two different inputs in a dynamically created form with JQUERY -
i writing program requires text box , textarea added dynamically form. using clone function purpose. unfortunately, cannot seem figure out how make each box have unique id. here jquery:
<script type="text/javascript"> $(document).ready(function() { $('#addbutton').click(function() { var num = $('.clonedinput').length; var newnum = new number(num + 1); var newelem = $('#form1').clone().attr('id', 'form' + newnum); newelem.children(':first').attr('id', 'name' + newnum).attr('name', 'name'); $('#form' + num).after(newelem); $(newelem).append( $(document.createelement('input')).attr({ id: 'mycheckbox' + newnum ,name: 'mycheckbox' + newnum ,value: 'myvalue' ,type: 'checkbox' }) ); }); }) </script>
and html:
<form method="post" form action="save3.php"> <div name="forms" id="form1" class="clonedinput" width="800px"> <label></label><textarea rows="4" cols="50" id="text1" class="text" name="text[]" ></textarea> <label></label><input type="text" id="title1" class="title" name="title[]"> </div> </form>
thank in advance
your problem seems you're renaming attribute values first child, label correctly gets id attribute value "name2":
newelem.children(':first').attr('id', 'name' + newnum).attr('name', 'name');
but can't see code takes care of textarea , input field in same manner. there several ways this, 1 being this:
$('#form' + newnum + ' textarea').attr('id','text' + newnum); $('#form' + newnum + ' input[type="text"]').attr('id','title' + newnum);
Comments
Post a Comment