1

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:

  • select li.open of #123 and change the class to li.pending

  • select the src attribute of the #123>li.open image and change it to "newpath"

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?

12
  • 2
    How about spaces? #123 > li.open Commented Sep 15, 2011 at 14:52
  • What was the outcome of your attempt? Commented Sep 15, 2011 at 14:53
  • First things first, you shouldn't use numeric id values. IDs should always start with a letter. Commented Sep 15, 2011 at 14:53
  • 2
    @Felix Kling: You mean #123 li.open? #123>li.open and #123 > li.open are the same. Commented Sep 15, 2011 at 14:56
  • 1
    @Ghazanfar in HTML5 IDs can be numbers. Commented Sep 15, 2011 at 14:57

2 Answers 2

3

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].

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.