Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
46
rated 0 times [  52] [ 6]  / answers: 1 / hits: 45857  / 15 Years ago, fri, october 23, 2009, 12:00:00

I start with this markup:



<div>
<span>
<label for=Item[0]>Item #1</label>
<input type=text value= name=Item id=Item[0]/>
</span>
</div>


On each button click I want to add another section exactly as the one above but with an incremented index.



<div>
<span>
<label for=Item[0]>Item #1</label>
<input type=text value= name=Item id=Item[0]/>
</span>
</div>
<div>
<span>
<label for=Item[1]>Item #2</label>
<input type=text value= name=Item id=Item[1]/>
</span>
</div>


I'm trying to use this javascript:



 $(document).ready(function(){

var count = <%= Items.Count - 1 %>;

$('#AddItem').click(function(e) {
e.preventDefault();
count++;

var tb = $('#Item[0]').clone().attr('id', 'Item[' + count + ']');

var label = document.createElement('label')
label.setAttribute('For', 'Item[' + count + ']')

$('#ItemFields').append(label);
$('#ItemFields').append(tb);
});
});


So a few issues:



Appending the label works, but my cloned textbox does not.



The label has no text. I can't seem to find the property for that. Can anyone tell me what it is?



I can't figure out how to wrap the label and textbox together in a div and span. If I try



$('#ItemFields').append('<div><span>' + label + tb + '</span></div>');


it outputs [object HTMLLabelElement] instead of the actual label. If I try to split them up into multiple append statements, it self terminates the div and span. I'm new to jQuery/Javascript, so I'm not quite sure what I'm doing wrong.


More From » jquery

 Answers
74

Example cloning the whole block (but adding an item class) because I think it's more robust for the future (if you change the html for an item e.g.) and handling the label's text:



<html>
<head>
<meta http-equiv=Content-Type content=text/html; charset=UTF-8 />
<title>Demo</title>
<script type=text/javascript src=jquery-1.3.2.min.js></script>
<script type=text/javascript>
$(document).ready(function(){
$('#AddItem').click(function(e) {
e.preventDefault();

var count = $('.item').length;
var id = 'Item[' + count + ']';
var item = $('.item:first').clone();
item.find('input:first').attr('id', id);
item.find('label:first').attr('for', id)
.html('Item #' + (1+count));
item.appendTo('#ItemFields');
});
});
</script>
</head>
<body>
<input type=button id=AddItem value=Add />
<div id=ItemFields>
<div class=item>
<span>
<label for=Item[0]>Item #1</label>
<input type=text value= name=Item id=Item[0]/>
</span>
</div>
</div>
</body>
</html>

[#98456] Monday, October 19, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emmaleet

Total Points: 203
Total Questions: 107
Total Answers: 98

Location: Cook Islands
Member since Thu, May 21, 2020
4 Years ago
emmaleet questions
Wed, Apr 28, 21, 00:00, 3 Years ago
Thu, Jan 7, 21, 00:00, 3 Years ago
Sat, Nov 28, 20, 00:00, 4 Years ago
Sat, Apr 18, 20, 00:00, 4 Years ago
;