New row unique id, will give rep...

Discussion in 'jQuery' started by MarcL, Feb 12, 2011.

  1. #1
    MarcL, Feb 12, 2011 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this:
    
    <!DOCTYPE html>
    <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
    	$("#add").click(function() {
    		var newname = 'name[' + $('#mytable tbody tr').length + ']';
    		$('#mytable tbody tr:last').clone(true).insertAfter('#mytable tbody tr:last');
    		$('#mytable tbody tr:last td input').attr('name', newname);
    		$('#mytable tbody tr:last td input').attr('id', newname);
    		return false;
    	});
    });
    </script>
    </head>
    <body>
    <a id="add">+</a></td>
    <table id="mytable" width="300" border="1" cellspacing="0" cellpadding="2">
    	<tbody>
    		<tr>
    			<td>Name</td>
    		</tr>
    		<tr class="person">
    			<td><input type="text" name="name[1]" id="name[1]" /></td>
    		</tr>
    	</tbody>
    </table>
    </body>
    </html>
    
    Code (markup):
     
    Cash Nebula, Feb 12, 2011 IP
    MarcL likes this.
  3. MarcL

    MarcL Notable Member

    Messages:
    4,265
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    215
    #3
    Thank you worked like a charm! Rep added :)
     
    MarcL, Feb 12, 2011 IP