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>";
    }
2
  • 1
    I guess this should work ele.innerHTML += "<p onclick ='openNewWindow(mv.exifImages[index])'> image" + index + "</p>"; Commented Feb 20, 2013 at 10:12
  • I get an Syntax error! Commented Feb 20, 2013 at 10:17

3 Answers 3

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>";
    }
Sign up to request clarification or add additional context in comments.

4 Comments

whats mv.exifImages[index]? is it a string?
Try now. I have added an modification
SyntaxError: unterminated string literal @
did you split a string across lines
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?

Comments

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.

6 Comments

SyntaxError: missing ) after argument list @
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.
you were not closing argument list with ')' in onclick
Pretty sure @Toms has got this, I'm voting up his answer.
The foor loop is inside another function. The object is to big to paste it here.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.