jQuery add image inside of div tag

Jquery

Jquery Problem Overview


I have a div tag

<div id="theDiv">Where is the image?</div>

I would like to add an image tag inside of the div

End result:

<div id="theDiv"><img id="theImg"  src="theImg.png" />Where is the image?</div>

Jquery Solutions


Solution 1 - Jquery

Have you tried the following:

$('#theDiv').prepend('<img id="theImg" src="theImg.png" />')

Solution 2 - Jquery

my 2 cents:

$('#theDiv').prepend($('<img>',{id:'theImg',src:'theImg.png'}))

Solution 3 - Jquery

$("#theDiv").append("<img id='theImg' src='theImg.png'/>");

You need to read the documentation here.

Solution 4 - Jquery

If we want to change the content of <div> tag whenever the function image()is called, we have to do like this:

Javascript

function image() {
    var img = document.createElement("IMG");
    img.src = "/images/img1.gif";
    $('#image').html(img); 
}

HTML

<div id="image"></div>
<div><a href="javascript:image();">First Image</a></div>

Solution 5 - Jquery

In addition to Manjeet Kumar's post (he didn't have the declaration)

var image = document.createElement("IMG");
image.alt = "Alt information for image";
image.setAttribute('class', 'photo');
image.src="/images/abc.jpg";
$("#TheDiv").html(image);

Solution 6 - Jquery

var img;
for (var i = 0; i < jQuery('.MulImage').length; i++) {
                var imgsrc = jQuery('.MulImage')[i];
                var CurrentImgSrc = imgsrc.src;
                img = jQuery('<img class="dynamic" style="width:100%;">');
                img.attr('src', CurrentImgSrc);
                jQuery('.YourDivClass').append(img);

            }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhill PaffordView Question on Stackoverflow
Solution 1 - JqueryTopher FangioView Answer on Stackoverflow
Solution 2 - JqueryKufView Answer on Stackoverflow
Solution 3 - JqueryjacobangelView Answer on Stackoverflow
Solution 4 - JqueryManjeet KumarView Answer on Stackoverflow
Solution 5 - JqueryEvan ParsonsView Answer on Stackoverflow
Solution 6 - JqueryAnand raoView Answer on Stackoverflow