JavaScript multiple iterations of the same div?

So I have a menu on a screen where there are several identical boxes - the only difference is that the boxes are numbered differently.

<div id="1">
    <select name="option1">
        <option value="1">foo</option>
        <option value="2">bar</option>
    </select>
</div>

<div id="2">
    <select name="option2">
        <option value="1">foo</option>
        <option value="2">bar</option>
    </select>
</div>

Each div is 900~ lines of code, and whenever I change I have to change each div - is writing a for loop and using document.write the best way to accomplish writing the div only once (and reduce file size)?

var divCode

for (var i=1; i < 7; i++){
 divCode +='
   <div id="'+i+'">
   <select name="option'+i+'">
   <option value="1">foo</option>
   <option value="2">bar</option>
   </select>
   </div>'
}
document.write('divCode')

Or is there is a better method in javascript? I imagine there is one, considering that writing 900 lines of concatenated string code is not fun (or efficient).

Answer:1

You can use a JavaScript templating engine like mustache to keep your code simple and maintainable.

Here an example (which also uses jQuery besides mustache):
HTML:

<script type="text/template" id="template_divs">
    {{#divs}}
    <div id="{{id}}">
        Hallo my name is {{name}}!
    </div>
    {{/divs}}
</script>

<div id="container">
</div>

JS:

var someDivs = {divs: [{id: 1, name: "Tom"},{id: 2, name: "Bob"},{id: 3, name: "Earl"}]};

var parsed = Mustache.render($('#template_divs').html(), someDivs);

$('#container').html(parsed);

I created a fiddle to demonstrate it for you: http://jsfiddle.net/2gYCz/

Answer:2

I'm trying to check when a div is reached from scrollbar with jquery. I read some similar question on stackoverflow, but all are on one div only. I have 4 div, with height: 100% and I want know when ...

I'm trying to check when a div is reached from scrollbar with jquery. I read some similar question on stackoverflow, but all are on one div only. I have 4 div, with height: 100% and I want know when ...

I'm trying to create a stripe customer using parse but can't seem to get the customer.id value from the response. var newCustomer; Stripe.Customers.create( card: request.params.cardToken, ...

I'm trying to create a stripe customer using parse but can't seem to get the customer.id value from the response. var newCustomer; Stripe.Customers.create( card: request.params.cardToken, ...

  1. creating stripe customer
  2. stripe creating customer object

I have a controller for uploading a file of any type. A condensed version of it is [HttpPost] public ActionResult Index (HttpPostedFileBase file, string selectedOrgName, string ...

I have a controller for uploading a file of any type. A condensed version of it is [HttpPost] public ActionResult Index (HttpPostedFileBase file, string selectedOrgName, string ...

I have two JSON objects that I want to merge in such a way that only the values of the properties in the first object that already exist are updated. I want to extend the first object with the second ...

I have two JSON objects that I want to merge in such a way that only the values of the properties in the first object that already exist are updated. I want to extend the first object with the second ...

  1. merge objects without overwriting
  2. javascript merge objects without overwriting
  3. merge array of objects without duplicates javascript
  4. javascript merge two objects without duplicates