ruzD
ruzD

Reputation: 555

Get the 'img' src recovery path

I have this code:

<table>
    <tr>
        <td>
            <img src='prod/images/1.jpg'>
        </td>
    </tr>
    <tr>
        <td>
            <img src='prod/images/2.jpg'>
        </td>
    </tr>
    <tr>
        <td>
            <img src='prod/images/3.jpg'>
        </td>
    </tr>
    <tr>
        <td>
            <img src='prod/images/4.jpg'>
        </td>
    </tr>
</table>

And I would like to recovery the value of src attribute and it assign to variable.

I found this:

var srcPath = item.closest("tr").find('img[src*="nameOFpath"]');

But this use to find not to assign value.

Any idea?

Upvotes: 1

Views: 62

Answers (3)

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

Use the jQuery methods prop() or attr() to get attribute value.

Use closest td to get specific img (if you use tr in closest it will return always first img "prod/images/1.jpg") :

var src_value = item.closest("td").find('img').prop('src');
//Or
var src_value = item.closest("td").find('img').attr('src');

Or if you already have a variable that contain all tds $tds use :

var src_value = $tds.closest("td").find('img').attr('src');

Hope this helps.

Upvotes: 1

M. Baza
M. Baza

Reputation: 44

You can simply do

var imgSrc = srcPath.attr("src");

and just like that the img src is going to be stored in the imgSrc variable

Upvotes: 0

Mehrad
Mehrad

Reputation: 1543

var srcPath = item.closest("tr").find('img[src*="nameOFpath"]');
srcPath.attr('src', 'yourNewValue');

Upvotes: 0

Related Questions