Reputation: 22050
I have a div placed outside the image div
<div id="" class="TextShow">Click</div>
<div style="">
<img src="images/file.jpg" id="2">
</div>
javascript/jquery code below to get the image path.
$('.TextShow').click(function() {
var ImageShowed = $('#2').attr('src');
alert(ImageShowed);
});
When I click on .TextShow the ImageShowed is NULL.
My question is, why am I not getting the image path.
Upvotes: 0
Views: 150
Reputation: 17940
You have some syntax problems:
your code should look like this:
$('.TextShow').click(function() {
var ImageShowed = $('#2').attr('src');
alert(ImageShowed);
});
here is a working sample: http://jsbin.com/wejozopeci/1/edit?html,js,output
Upvotes: 1
Reputation: 5849
You are missing a {
and have added an extra [
which as nothing to do here.
Here's your corrected code:
jQuery(function ($) {
$('.TextShow').click(function () { // << this was missing
var ImageShowed = $('#2').attr('src'); // << removed the extra [
alert(ImageShowed);
});
});
Also, think of wrapping your code inside jQuery(function($) { ... });
to be sure your jQuery code is executed after the DOM is loaded.
Upvotes: 2