Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to insert javascript variable value in html element inside javascript.. But it's not working properly.. following are my code..

 window.onload = function() {
    var image=document.getElementById('img').value;
        var img = '<div id="pic"><img src="image" width="200" height="108" /><p></p></div>';
      }

This is the function. Here i'm getting image in variable image.. i'm trying to assign this image to in one div id pic.. there i'm assigning src="image" which i got in var image.. But its not working..

share|improve this question

3 Answers

You need to insert the value into the string:

var img = '<div id="pic"><img src="' + image + '" width="200" height="108" /><p></p></div>';
share|improve this answer
 
Thank you .. :-) –  Dhinesh.B Jun 21 '12 at 5:44

it should be like this:

 var img = '<div id="pic"><img src="'+image+'" width="200" height="108" /><p></p></div>'; 
share|improve this answer

I think you should do this instead:

window.onload = function() {
    var image=document.getElementById('img').value;
    document.getElementById('pic').innerHTML='<img src="'+image+'" width="200" height="108" /><p></p>';
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.