Geo1986
Geo1986

Reputation: 65

JQuery find an img src

With JQuery I want to be able to get the img src link.

   <tbody>
            <tr>
                <td headers="header_0 header_1">&#160;<img
                    src="https://this-link-here.com" width="26"
                    height="24" alt="" border="0" /></td>

I have tried

   var data = $(data).find("[headers='header_0 header_1']");

but this is not getting the link. Similarly I have tried adding .text(), and .html() but still no luck.

Can anyone help? The HTML cannot be changed.

Upvotes: 1

Views: 21772

Answers (3)

PlantTheIdea
PlantTheIdea

Reputation: 16369

You can use either the .attr() function:

var data = $(data).find("[headers='header_0 header_1']").find('img').attr("src");

Or the vanilla JS .src function, if there is only one with those headers:

var data = $(data).find("[headers='header_0 header_1']").find('img').get(0).src;

The vanilla JS way is faster, only reason I threw it in.

Upvotes: 7

emerson.marini
emerson.marini

Reputation: 9348

Do it like this:

var data = $("img", "td[headers='header_0 header_1']").attr("src");

Demo: http://jsfiddle.net/udeXR/2/

Upvotes: 2

Itay
Itay

Reputation: 16785

Use the .attr() function.

var data = $("td[headers='header_0 header_1'] img").attr("src");
  • P.S - I've changed the selector because something doesn't look right in var data = $(data) etc.. You can use a different selector if you wish.

.attr()

.attr( attributeName )

Returns: String

Description: Get the value of an attribute for the first element in the set of matched elements.

Upvotes: 2

Related Questions