1

I am trying to retrieve data from an img element inside a html web page using jQuery.

I know from the start that there is only 1 image but when I run the following code, I get 2 alert boxes. They contain the same information..

Does anyone know what i am doing wrong?

$("#tableX td").find("img").each(function() { if ($(this).data("apple") == "orange") { alert($(this).attr("src")); } }); 

Thanks.

UPDATE:

DOM output..

<td id="tdP4" align="center" style="border-top-width: 2px; border-right-width: 2px; border-bottom-width: 2px; border-left-width: 2px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(201, 201, 201); border-right-color: rgb(201, 201, 201); border-bottom-color: rgb(201, 201, 201); border-left-color: rgb(201, 201, 201); "><img id="imgP4" src="/images/t/00.jpg" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; width: 63px; height: 103px; display: block; background-color: rgb(71, 7, 79); " alt="00"></td> 
15
  • 3
    Seems like there are more than two images in the table ;) The code is fine. Do you have a link to the actual page? Commented Jul 13, 2011 at 7:31
  • 5
    can you show the html? also are the two alerts show same src Commented Jul 13, 2011 at 7:32
  • it would be difficult.. its very heavy on javascript and all the dom manipulation is done there. looking at the source wouldnt help. its just a basic table with some tr's and td's and 1 image per td.. Commented Jul 13, 2011 at 7:34
  • 1
    okhay the alert is being shown because one img per td there must be two tds... Commented Jul 13, 2011 at 7:35
  • 2
    There are two possible reasons: Either you have several rows containing an image (the selector selects all images in the whole table), or the code is run twice. Commented Jul 13, 2011 at 7:52

3 Answers 3

1

There are two possible reasons:

  • Either you have several rows containing an image (the selector selects all images in the whole table)
  • or the code is run twice.
Sign up to request clarification or add additional context in comments.

Comments

1

maybe try a more specific selector

$("#tableX td > img").each(function() { if ($(this).data("apple") == "orange") { alert($(this).attr("src")); } }); 

EDIT: your <img> tag is left open

2 Comments

sorry this hasnt changed anything.
Regarding the image tag: Als long as this is HTML and not XHTML, this is fine.
0

You say

only one table, one td per tr, and 1 image!

This allows for multiple tr elements, each with a td and an img inside.

The $("#tableX td").find("img").each will find all img elements inside the #tableX. It doesn't matter if they are in different rows (tr).

1 Comment

yes but like i said, there is only one image. and the alert box is telling me the source, so i know it is the same one..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.