X10nD
X10nD

Reputation: 22050

Click on DIV to show image path in an alternate DIV

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

Answers (2)

Tomer
Tomer

Reputation: 17940

You have some syntax problems:

  1. first line missing '{' at the end
  2. Second line contains extra '[' that should be removed

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

D4V1D
D4V1D

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.

Working JSFiddle

Upvotes: 2

Related Questions