0

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(content,
    'open_window',
    'menubar, toolbar, location, directories, status, scrollbars, resizable, dependent, width=640, height=480, left=0, top=0')
},

for (var index in mv.exifImages) {
        ele.innerHTML += "<p onclick = openNewWindow(mv.exifImages[index]> image" + index + "</p>";
    }
Jacob
  • 3,580
  • 22
  • 82
  • 146

3 Answers3

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>";
    }
999k
  • 6,257
  • 2
  • 29
  • 32
0

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?

Community
  • 1
  • 1
Salvatorelab
  • 11,614
  • 6
  • 53
  • 80
0

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.

Frances McMullin
  • 5,516
  • 2
  • 18
  • 19
  • SyntaxError: missing ) after argument list @ – Jacob Feb 20 '13 at 11:05
  • on which line? either show a complete code block or tell me where you intend to move your for loop to. the openWindow function is clearly part of an object, where's the rest? You need to close the object. Also, it would be wise to have a semi-colon after the call to window.open, as I have added in my answer. – Frances McMullin Feb 20 '13 at 11:10
  • you were not closing argument list with ')' in onclick – 999k Feb 20 '13 at 11:12
  • Pretty sure @Toms has got this, I'm voting up his answer. – Frances McMullin Feb 20 '13 at 11:14
  • The foor loop is inside another function. The object is to big to paste it here. – Jacob Feb 20 '13 at 11:46
  • I just want a code that generates links which open contents of my exifImages array at proper index in a new window. – Jacob Feb 20 '13 at 11:47