Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would go along the lines of:</p> <pre><code>var i = 0, m = myJSONObject.model, l = m.length, models = document.getElementById("models"); for(; i &lt; j; i++) { var model = m[i]; var create_div = document.createElement("div"); create_div.id = "model_id" + i; create_div.innerHTML = "&lt;a&gt;" + model.model_name + "&lt;/a&gt;"; models.appendChild(create_div); } </code></pre> <p>Unless you specifically need to do something to the anchor itself (other than set its innerHTML), there's no need to create a reference to an element for it. If you do need to do something specific to that anchor, then in that case have this, instead:</p> <p>EDIT: As per your comment, you DO want to do something to the anchor, so go with this (now updated) option - assuming the anchor will always be a child of the div that has the ID you require. The reason "model_id" + i is being put in as a string is because that is exactly what is being passed into the HTML - the document has no clue what "i" is outside of javascript:</p> <pre><code>var i = 0, m = myJSONObject.model, l = m.length, models = document.getElementById("models"); for(; i &lt; j; i++) { var model = m[i]; var create_div = document.createElement("div"); var create_anchor = document.createElement("a"); create_div.id = "model_id" + i; create_anchor.innerHTML = model.model_name; if(window.addEventListener) { create_anchor.addEventListener("click", function() { getModelData(1, this.parentNode.id); }, false); } else { create_anchor.attachEvent("onclick", function() { getModelData(1, this.parentNode.id); }); } create_div.appendChild(create_anchor); models.appendChild(create_div); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload