4

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

3 Answers 3

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

Comments

0

it should be like this:

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

Comments

0

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

Comments

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.