PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 5, 2022

[FIXED] How to display picture into div with jquery

 May 05, 2022     html, image, jquery     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing