Nate P
Nate P

Reputation: 69

JQuery - Replace DIV's content with Image

I have a DIV called MainDiv. I like to replace the content of the DIV with an image on click of a button. I tried in order to replace it within the click even in Jquery but something happened.

    $('#MainDiv').attr('src', 'images/Img1.jpg ');

Am I missing anything?

Upvotes: 1

Views: 6819

Answers (3)

Syed Ali Taqi
Syed Ali Taqi

Reputation: 4974

<div> <img src="" id="img"/> </div>
<input type="button" id="btn" value="Click">


$(document).ready(function () {
    $( "#btn" ).click(function() {
        $("#img").attr("src", src);
   });
});

DEMO

Upvotes: 0

T J
T J

Reputation: 43156

You can use replaceWith()

$('#MainDiv').replaceWith("<img id='MainDiv' src='images/Img1.jpg'/>");

Upvotes: 0

Mardoxx
Mardoxx

Reputation: 4482

$("#MainDiv").html( "<img src='images/Img1.jpg'>" );

Upvotes: 2

Related Questions