Thanks for your suggestion. In another situation, I need to generate Id for each individual entry of a collection data. But I don’t know how create one inside of a loop like this case.
tags need the same onclick attached then simply looping over them using getElementsByClassName and adding the click listener that way would be easier without even needing any ids.
A code is needed for deleting an individual entry, but not all of them with one click. The getElementsByClassName might be helpful, but some sort of Id or key is still needed to pass into a Javascript method. If so, we walk a cycle and go back to the original question.
This should be quite easy. Your success call (response) should be the script you want to embed. Append it to any div with the appendChild or insertBefore functions should work. I even override the whole existing script completely with this method.
Note that $(“#specificationList”).html(respContent); isn’t worked. Don’t use this method.
If I understand your words correctly, I should have something like the followings:
success : function(response) {
var specifications = response.specifications;
var respContent = '';
for(var i in specifications){
var dtNode=document.createElement("dt");
var dtTextNode=document.createTextNode(i);
dtNode.appendChild(dtTextNode);
var ddNode=document.createElement("dd");
var ddTextNode=document.createTextNode(specification[i]);
ddNode.appendChild(ddTextNode);
document.getElementById("specificationList").appendChild(dtNde),appendChild(ddNode);
}
}
If the above is correct, I don’t get how to have the javascript function call.
Note that $(“#specificationList”).html(respContent); isn’t worked. Don’t use this method.
I don’t understand your this statement. I use it to get the data show up on the html page.
Yes, you have to add this too:
var script = document.createElement( ‘script’ );
script.type = ‘text/javascript’;
script.setAttribute(‘type’,‘text/javascript’);
script.text = respContent;
$(‘#specificationList’).appendChild(script);
Something like that.
Sorry, I found it’s disturbing to write code in this new interface.
if the event is registered through JavaScript, you can get the “evented” (clicked) element via this. from there onward you can transition relatively through the DOM.
If it’s just quote escaping that’s the problem, you’re very close.
If the specification objects have an id you can just spit that out in the spot can’t you? Or if your deleteSpecification method is expecting the count from the loop just output i. If the id’s are numbers you won’t need quotes around it but if you do just escape them with \
'onclick="deleteSpecification(' + specifications[i].id + ')"'
'onclick="deleteSpecification('+ i +')"'
'onclick="deleteSpecification(\''+ i +'\')"'
The other posters have tried telling you that’s there’s easier ways to do this, and they’re right. But you’ll still need to most likely output an id on the elements for those scripts to hang off.