netman97
netman97

Reputation: 111

Can I add <img> tag using insertAfter() in jQuery?

Can I add <img> tag using insertAfter in jQuery?

$("<img src="/static/img/icinfo.png"/>").insertAfter('#id_input_field');

It seems not work.

Upvotes: 0

Views: 186

Answers (5)

shiva kumar
shiva kumar

Reputation: 121

$('<img src="/static/img/icinfo.png"/>').insertAfter('#id_input_field');

Try it this way. May work

Upvotes: 0

Lix
Lix

Reputation: 47956

This is not working because you are using quotes within quotes on your first selector.

$("<img src="/static/img/icinfo.png"/>")

Try using a combination of both single and double quotes:

$("<img src='/static/img/icinfo.png'/>")

Here I have used double quotes on the outer string (the actual jQuery selector) and single quotes on the inner src attribute value. You could also use them the other way around: single on the outside and double on the inside.

The quotes tell JavaScript what is a string and what is not. The code you have opens a string with " and then closes it when you define the src attribute.


You can see this clearly by how the syntax is highlighted - even here on Stack Overflow. See how the color of the text changes? The text marked in red is recognized as a string and the rest is not.

Upvotes: 2

ant
ant

Reputation: 22948

Your code is ok but it's not properly quoted, consider this :

$('<img src="/path/to/image.jpg">').insertAfter("#id_input_field");

Alternative as well (create tag):

var img = $('<img/>').attr('src', '/path/to/image.jpg');

img.insertAfter("#id_input_field");

Upvotes: 1

I A Khan
I A Khan

Reputation: 8841

Try This

$("<img src='/static/img/icinfo.png'/>")

Upvotes: 0

Dhaval Marthak
Dhaval Marthak

Reputation: 17366

you are having the quotes "' misplaced, you can use single quotes

$('<img src="/static/img/icinfo.png"/>').insertAfter('#id_input_field');

Upvotes: 1

Related Questions