Sotos
Sotos

Reputation: 146

jQuery nested selector for an 'src' image URL

I have the below HTML structure.

    <td id="123">
      ...
    <td>
        <ul>
            <li class"open"><img src="path" /></li>
            ...
        </ul>
    </td>
</tr>
<td id="125">
...

I want to do this:

I tried to select them by:

$('#123>li.open img').attr("src");
...to add the new src

$('#123>li.open').removeClass("open").addClass("pending");

How can I fix this problem?

Upvotes: 1

Views: 773

Answers (2)

voigtan
voigtan

Reputation: 9031

Your CSS-selector says: find li.open as a direct child of #123, but that's never true, because you have at least a td, ul under your #123 element.

$('#123 li.open>img').attr("src");

is more true in the markup you have posted above. img is a direct child of li.open.

Upvotes: 0

Alnitak
Alnitak

Reputation: 339806

The > CSS operator enforces a direct parent-child relationship. You have a <td> and <ul> element in between.

Remove the > and all should be fine.

[and per comments, for backwards compatibility don't use numbers for IDs].

Upvotes: 3

Related Questions