0

I am trying to find the attribute of an element in jquery, but it keeps returning undefined. This is the HTML I have,

<div class="item-image-wrapper">
  <a href="/Money-Headphones-item?id=283751750">
    <img title="Money Headphones" alt="Money Headphones" class="original-image" 
       src="http://t2.rbxcdn.com/ef3e40344e3d5fd021f94aaa71593c76">
    <img src="http://images.rbxcdn.com/b84cdb8c0e7c6cbe58e91397f91b8be8.png" alt="New">
   </a>
</div>

Firstly, how would I use jquery to find the "a" element because it has no name, then how would I find the second image of "a" and alert the attribute "alt"

I currently have:

$(data).find("a").find("img:eq(1)").attr("alt")

but it doesnt work. Can someone help please.

2
  • Maybe posting more code (specifically parents of <a>) will help us in helping you better. Commented Aug 15, 2015 at 13:16
  • 1
    Can you use the href attribute for identification? What about div.item-image-wrapper > a? Commented Aug 15, 2015 at 13:21

3 Answers 3

1

:eq() is 0-based indexing,

Use

$(data).find("a").find("img:eq(0)").attr("alt")

However you can use child selector and no need to use :eq() as you have only 1 img element

$(data).find("a > img").attr("alt")
Sign up to request clarification or add additional context in comments.

2 Comments

"...as you have only 1 img element" It would be fine with more than one as well, provided you just wanted the attribute from the first one.
Thanks.. YAYAYAYAYAYAYYAAYA
0

You can use

$("a > img").attr("alt")

1 Comment

Doesn't work. Remember the anchor tag has no name. Just a href
0

you can also use :first

$(data).find("a").find("img:first").attr("src")

Jsbin

1 Comment

Thanks you Very mcuch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.