Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOM appendChild to insert images

Tags:

javascript

dom

I have this code that creates links.

   /* Create a link to activate the tab */
    DOM_a = document.createElement("a");
    DOM_a.appendChild(document.createTextNode(t.headingText));
    DOM_a.href = "javascript:void(null);";
    DOM_a.title = t.headingText;
    DOM_a.onclick = this.navClick;

I need to add an image to the link, but when I try to add the image code:

<img src="typo3conf/ext/ori_proyectos/res/images/interes.png">

I get:

Link<img src="typo3conf/ext/ori_proyectos/res/images/interes.png">

And not: Link[*_*]

Where [*_*] is the image.

The source code display this:

&lt;img src="typo3conf/ext/ori_proyectos/res/images/interes.png"&gt;

I don't know how to write it.

Thanks.

like image 751
Memochipan Avatar asked Oct 28 '11 17:10

Memochipan


People also ask

What does appendChild () method perform?

The appendChild() method of the Node interface adds a node to the end of the list of children of a specified parent node. If the given child is a reference to an existing node in the document, appendChild() moves it from its current position to the new position.

Should I use append or appendChild?

Difference between appendChild() and append()append() also allows you to append DOMString objects, and it has no return value. Further, parentNode. appendchild() allows you to append only one node, while parentNode. append() supports multiple arguments - so you can append several nodes and strings.


1 Answers

You should create the image using own DOM methods too:

Something like this:

var DOM_img = document.createElement("img");
DOM_img.src = "typo3conf/ext/ori_proyectos/res/images/interes.png";

DOM_a.appendChild(DOM_img);

A working example here.

like image 162
Marcelo Assis Avatar answered Oct 07 '22 11:10

Marcelo Assis