Adding To Innerhtml Correct Syntax
What is the correct syntax if I want to add content to an element using innerHTML. Below is my non working example: openNewWindow: function(content) { popupWin = window.open(con
Solution 1:
i think it is. variable index
has local scope
for (var index in mv.exifImages) {
ele.innerHTML += "<p onclick = 'openNewWindow(\"" + mv.exifImages[index] + "\")'> image" + index + "</p>";
}
Solution 2:
Use appendChild
:
var myelement = document.getElementById("myelement");
myelement.appendChild( document.createTextNode("Example text inside myelemen") );
This is better that overwriting innerHTML, as it preserves onclick events for example: Is it possible to append to innerHTML without destroying descendants' event listeners?
Solution 3:
innerHTML is not your problem, your code is lacking context.
openNewWindow: function(content) {
popupWin = window.open(content,
'open_window',
'menubar, toolbar, location, directories, status, scrollbars, resizable, dependent, width=640, height=480, left=0, top=0');
// call to window.open ended here
},
// judging by the definition of openNewWindow and the comma above,// we are inside an object definition!// you cannot embed a for loop inside an object definition!!!for (var index in mv.exifImages) {
ele.innerHTML += "<p onclick = openNewWindow(mv.exifImages[index]> image" + index + "</p>";
}
Put your for loop somewhere sensible, like inside a function, or actually show us the error you are getting.
Post a Comment for "Adding To Innerhtml Correct Syntax"