admin管理员组

文章数量:1302273

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..

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 edited Jun 23, 2012 at 14:08 Gordon 317k76 gold badges546 silver badges565 bronze badges asked Jun 21, 2012 at 5:40 Dhinesh.BDhinesh.B 4814 gold badges10 silver badges21 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

You need to insert the value into the string:

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

it should be like this:

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

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>';
}

本文标签: I want to assign javascript variable to html element inside javascriptStack Overflow