0

How do I write this in jquery? I want to display a image 600x400 inside the div block on click?

<script>
function displayImage() {
 document.getElementById("demo").innerhtml ="image.jpg";

}
</script>

<form>

<input type="submit" onClick="displayImage()">
</form>

<div id="demo">

</div>
2
  • I'm assuming that this is just a typo in putting the question together, but 'script' is misspelled in the opening <script> tag. Commented Oct 18, 2010 at 19:47
  • Why using a form for... that ? :O Commented Oct 19, 2010 at 9:57

3 Answers 3

3
$("#demo").html("image.jpg");

However, "image.jpg" is not HTML. So you should just use:

$("#demo").text("image.jpg");

If you are trying to add an image to the div, use:

$("#demo").html('<img src="image.jpg" />');
Sign up to request clarification or add additional context in comments.

4 Comments

nice. how would i do it say if i click a link and I have 2 to 10 links and I want all the itemsor images from link to show up in the demo div block? -thanks
@mcgrailm, you need to get out more ;)
@user244394, you could do $(".SomeClass").append('<img src="image.jpg" />'); Or something like that. You selector and data you are appending would need to change.
@Abe that maybe so but to display 600x400 text on a web page ? can't tell me that's not funny
1

Assuming you want only the image to be displayed within the #demo (and also assuming you want this to happen on form submission):

$('form').submit(
    function() {
        $('#demo').html('<img src="path/to/image.jpg" />');
        return false;
    }
);

If, instead, you want to add an image to the #demo (though still assuming this is on form submit):

$('form').submit(
    function() {
        $('<img src="path/to/image.jpg" />').appendTo('#demo');
        return false;
    }
);

Just a quick note, but it seemed worth mentioning:

  1. the above all need to be enclosed by a $(document).ready(/*...*/) (or equivalent).
  2. the onClick inline click-handler is no longer required if the above is used, and might actively complicate matters if it remains in place.

Comments

0

assuming you want the image to be displayed... change your html to look like so:

<input type="submit" id="SubmitButtonId" />
<img id="demo" src="someimage.jpg" />

and your javascript to:

<script>

    $(function(){
        $('#SubmitButtonId').live('click', function(){
            $('#demo').src('image.jpg');
        });
    });
</script>

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.