0

in my project i have stored in a variable (htmlG) an html code like this:

<img src="http://mygrtew.imm.com/graph?&o=f&c=1&y=q&b=ffffff&n=666666&w=450&h=250&r=1m&u=www.test.com" width="450" height="250"/>

and i would like to insert dinamically in a div that i create with DOM this image directly

var htmlG = response.testg;
    var divAgraph = createElement('div', 'divAgraph', 'divAgraphcss');
    var oImgG = createElement('img');
    oImgG.setAttribute('src',htmlG);
    divAgraph.appendChild(oImgG);

but fail, probably because in var htmlG at the beginning there is correct?

How can icreate my img with these parameters?

thanks in advance

8
  • What is the createElement function? Commented Nov 16, 2013 at 10:39
  • var htmlG = "<img src="http://mygrtew.imm.com/graph?&o=f&c=1&y=q&b=ffffff&n=666666&w=450&h=250&r=1m&u=www.test.com" width="450" height="250"/>" Is it like this you assigned?? Commented Nov 16, 2013 at 10:41
  • @lonesomeday he is probably referring to a wrapper of the method defined in document. Commented Nov 16, 2013 at 10:41
  • @JackieXu Presumably so, but it would be nice to have that confirmed, since he isn't using the normal signature of that method. Commented Nov 16, 2013 at 10:42
  • yes is my method like this: Commented Nov 16, 2013 at 10:58

2 Answers 2

1

Hope this helps:

var htmlG = response.testg;
var divAgraph = document.createElement('div');
divAgraph.innerHTML = htmlG 

I assume you are working in an environment that provides DOM. (node.js does not)

Sign up to request clarification or add additional context in comments.

Comments

1

You try to set the src attribute of your <img> element

oImgG.setAttribute('src',htmlG);

yet htmlG does not contain the src attribute content but a complete HTML img element as well.

So instead of your current

var htmlG = '<img src="http://mygrtew.imm.com/graph?&o=f&c=1&y=q&b=ffffff&n=666666&w=450&h=250&r=1m&u=www.test.com" width="450" height="250"/>';
oImgG.setAttribute('src', htmlG);

you probably want to

var htmlG = 'http://mygrtew.imm.com/graph?&o=f&c=1&y=q&b=ffffff&n=666666&w=450&h=250&r=1m&u=www.test.com';
oImgG.setAttribute('src', htmlG);

If you want to set the width and height attributes as well, you will have to call setAttribute for those two separately.


If you have to work with the entire HTML code for the element - as the dereferencing of response.testg suggests, you can use innerHTML to set the content of your div - and not call setAttribute at all.

divAgraph.innerHTML = htmlG 

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.