'How to display picture into div with jquery
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 1:[1]
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
Solution 2:[2]
You're on the right track. There's basically two ways: 1) add the <img>
tag to the div, or 2) change the background-image with JS/css.
Change:
$("#imageBox").html("<img src=' + this.href + '>");
To:
$("#imageBox").html("<img src=\"" + this.href + "\">");
Your quotes were wrong.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Adam Plocher |