简体   繁体   中英

I want to assign javascript variable to html element inside javascript

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

您需要将值插入字符串:

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

它应该是这样的:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM