1

I have following div tag with child tags.

<div class="span6"> <div class="control-group"> <label class="control-label" for="inputEmail">Item Name</label> <div class="controls"> <input type="text" id="inputEmail" class="input-xlarge" placeholder="Enter item name here..."> </div> </div> </div> 

And I wanna append child <div class="control-group"></div> to parent <div class="span6"></div> according to the array. So I have written the javascript is as below.

$.each(categories, function (k, elem) { var options = ''; var options = ' <div class="control-group"> <label for="StockCategory">Category</label> <div class="controls"> <select class="input-xlarge get-category"> '; options +='<option value="">Select Category</option>'; $.each(data, function(key, value) { options += '<option value="' + key + '">' + value + '</option>'; }); options +=' </select> </div> </div>'; $("div.span6").append(options); } 

So I wanted to append these child divs according to array but Its not happening. So I needed the following result.

<div class="span6"> <div class="control-group"> <label class="control-label" for="inputEmail">Item Name</label> <div class="controls"> <input type="text" id="inputEmail" class="input-xlarge" placeholder="Enter item name here..."> </div> </div> <div class="control-group"> <label class="control-label" for="inputEmail">Item Name</label> <div class="controls"> <input type="text" id="inputEmail" class="input-xlarge" placeholder="Enter item name here..."> </div> </div> <div class="control-group"> <label class="control-label" for="inputEmail">Item Name</label> <div class="controls"> <input type="text" id="inputEmail" class="input-xlarge" placeholder="Enter item name here..."> </div> </div> <div class="control-group"> <label class="control-label" for="inputEmail">Item Name</label> <div class="controls"> <input type="text" id="inputEmail" class="input-xlarge" placeholder="Enter item name here..."> </div> </div> <div class="control-group"> <label class="control-label" for="inputEmail">Item Name</label> <div class="controls"> <input type="text" id="inputEmail" class="input-xlarge" placeholder="Enter item name here..."> </div> </div> </div> 

Please help me to get it. The work is more appreciated.

2
  • i don't understand...So I needed the following result....and i don't see anything with StockCategory and so on... are you saying u just need to clone the first div ... seeing you result looks like that is what u want... Commented Feb 13, 2013 at 10:46
  • and where is your data for the second each loop ??? Commented Feb 13, 2013 at 10:48

4 Answers 4

1

Below jquery will do the needful.

<script type="text/javascript"> $(document).ready(function(){ var data = { val1 : 'text1', val2 : 'text2', val3 : 'text3' }; var options = ''; $.each(data, function(key, value) { /* options = '<div class="control-group"><label for="StockCategory">Category</label><div class="controls"><select class="input-xlarge get-category">'; options += '<option value="">Select Category</option>'; options += '<option value="' + key + '">' + value + '</option>'; options +='</select></div></div>'; */ options = '<div class="control-group"><label class="control-label" for="' + key + '">' + key + '</label><div class="controls">'; options +='<input type="text" id="' + key + '" class="input-xlarge" placeholder="' + value + '">'; options +='</div></div>'; $("div.span6").append(options); }); }); </script> 
Sign up to request clarification or add additional context in comments.

Comments

1

The first thing I would look at is that span6 is missing a closing </div> - does that fix it?

Comments

1

Try to put the append code outside the $.each

var options = ''; $.each(categories, function (k, elem) { var options = ' <div class="control-group"> <label for="StockCategory">Category</label> <div class="controls"> <select class="input-xlarge get-category"> '; options +='<option value="">Select Category</option>'; $.each(data, function(key, value) { options += '<option value="' + key + '">' + value + '</option>'; }); options +=' </select> </div> </div>'; } $("div.span6").append(options); 

Comments

1

try to change your script as

var options = ''; $.each(categories, function (k, elem) { options += ' <div class="control-group"> <label for="StockCategory">Category</label> <div class="controls"> <select class="input-xlarge get-category"> <option value="">Select Category</option> '; $.each(data, function(key, value) { options += '<option value="' + key + '">' + value + '</option>'; }); options +=' </select> </div> </div>'; } $("div.span6").append(options); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.