0

I'm trying to get the image SRC using this: var SRCNow = $('#'+ID+'.item').attr("src");

on this:

<a id='17' href='#' name='item' class='tilelink'><img title='17' id='17' style='background:red'; class='item' src='line_tile/t17.png' /></a>

So when I click on that image by doing on click image...: var ID = $(this).attr("id");

and then I do alert(SRCNow); and I get undefined.... when it should say the SRC.

test
  • 17,706
  • 64
  • 171
  • 244

3 Answers3

5

The problem is that you have two elements with the same id. This is not allowed.

http://www.w3schools.com/tags/att_standard_id.asp:

Definition and Usage

The id attribute specifies a unique id for an HTML element.

The id must be unique within the HTML document.

Talljoe
  • 14,593
  • 4
  • 43
  • 39
1

First, an html id must begin with a letter ( What are valid values for the id attribute in HTML? ).

Your specific problem is, you must add a space between the id and the class item, like this:

var SRCNow = $('#'+ID+' .item').attr("src");

Community
  • 1
  • 1
morgar
  • 2,339
  • 17
  • 16
0

The problem is that you have a LINK as well as image with the ID 17. IDs should be unique. Also, IDs starting with a number are not valid (as far as I remember).

First make your IDs unique. Then you don't need the class filter when you're trying to grab the image src.

<a id='link-17' href='#' name='item' class='tilelink'>
    <img title='17' id='img-17' style='background:red'; class='item' src='line_tile/t17.png' />
</a>

var SRCNow = $('img-17').attr('src');

Or you can loop through your images

var srcs = []; //array of srcs
$('images.item').each(function(){
   srcs.push($(this).attr('src'));
})
JohnP
  • 49,507
  • 13
  • 108
  • 140