Thursday, May 5, 2022

[FIXED] How to display picture into div with jquery

Issue

Is anyone can tell me how can I modify below code to display picture into a div when user click in link ?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<script type="text/javascript"
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
    $("a").click(function() {
        $("#imageBox").html("<img src=' + this.href + '>");
    });
</script>
</head>
<body>
    <a href="background.jpg" class="sideLoad">Link to image 1</a>
    <a href="background.jpg" class="sideLoad">Link to image 2</a>
    <div id="imageBox"></div>
</body>
</html>

Solution

Your code should be in .ready

$(function(){
    $("a.my-class").click(function(e) {
        e.preventDefault();
        $("#imageBox").html('<img src="' + this.href + '" />');
    });
})

This will enable the feature for a elements with class myclass, others will function normally

Demo: Fiddle



Answered By - Arun P Johny
Answer Checked By - Marilyn (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.