Reputation: 1507
I have the below anchor
<a class="images" href="/a/b/c/abc.jpg" title="@fileName">@fileName</a>
on click of this I am setting src of the img tag as below:
$("a.images").click(function () {
var _src = $(this).attr('href');
("#img").attr("src", _src);
});
Here the problem is upon setting the image, the url is changed to the image url i.e., localhost:1234//a/b/c/abc.jpg. However, the url should not change, it should just display the image in a div.
can somebody advise?
Upvotes: 0
Views: 563
Reputation: 133403
You need to use either event.preventDefault()
or return false
, this will prevent the default action of the event from getting triggered.
Use
$("a.images").click(function (event) {
event.preventDefault(); //Use it
var _src = $(this).attr('href');
$("#img").attr("src", _src);
//return false; Or you can use it
});
Upvotes: 1
Reputation: 9637
There is one mistake ("#img").attr("src", _src)
; you have to use $("#img").attr("src", _src);
$("a.images").click(function (e) {
e.preventDefault();
$("#img").attr("src", $(this).attr('href'));
});
e.preventDefault(); it is used to stop the default action
Upvotes: 2