views:

252

answers:

5

Hi, I'm using selectorgadget to help me select elements on the document. But when I used it on jQuery it doesn't work. The nth-child selector seems not to work for me.

<div id = "wrap">
<div class = "book">
    <div class = "chapter">
     <p class = "text">
      <table>
       <tbody>
        <tr>
         <td>Title</td>
         <td>
          <b>jQuery</b>
         </td>
        </tr>
       </tbody>
      </table>
     <p>
    </div>
</div>
</div>

The HTML is above. There are many book class. The div wrap is the wrapper that enclosed them all. Selector Gadget said that when I use:

$("#wrap tr:nth-child(1) b") It should select the TD element with the <b> tag in it. So it should select "jQuery".

I tried it, but it didn't work. When I tried:

var query = $("#wrap tr:nth-child(1) b").text();
alert(query);

The browser sends an alert with no text. And when I tried:

var query = $("#wrap tr:nth-child(1) b").html();
alert(query);

The browser sends a NULL alert.

What's wrong?

A: 

have you tried

modified, i first didnt notice the tr:nth-child, there is no 1 child tr in that table, but there is a td at index 1

var query = $(".text td:nth-child(1) b").html();
alert(query);
John Boker
Yeah, it return null. Whenever I use the nth-child. I get the null everytime.I'm using greasemonkey with jQuery. Perhaps I can't use nth-child in greasemonkey?
Keira Nighly
John I can use .text tr b. But I want to know why I can't use the nth-child. Because I need to select other more complicated elements in the document that requires the use of nth-child.
Keira Nighly
instead of tr use td
John Boker
I tried it also, still no luck.. Is there any alternative to nth-child?
Keira Nighly
not that i know of, im not sure what the problem is.
John Boker
The nth child is the problem I think. Everytime I use nth child it always returns null.
Keira Nighly
is the index 0-based?
harto
no, Xpath is 1 indexed for some reason
nickf
A: 

In testing your sample in Firefox 3 and IE8, I was able to get back the text you expected using your selector after one tweak - I added an ID of "wrap" to your table element:

<table id="wrap">

Once that was added in, your selector seemed to work just fine. Is there a particular platform you are having trouble with?

Goyuix
I'm using Greasemonkey. It is not my HTML document.. Maybe I need to add a class to all the table?
Keira Nighly
A: 

I just tried your example code and it worked straight away. What version of jQuery are you using? It works in 1.3.2

Here's the working example: http://jsbin.com/uzuzu/edit

nickf
I'm using 1.3.2.Hmm, I think the problem is in Greasemonkey.. I'm using Greasemonkey with jQuery.
Keira Nighly
how are you referencing jQuery? are you using unsafeWindow.jQuery ?
nickf
+2  A: 

I've tried this with FF 3.0.10, jQuery 1.3.2, greasemonkey 0.8.2

var query = $("#wrap tr:nth-child(2) b").text();
returns "jQuery"

the 2 in nth-child() does the trick!! Index for nth-child starts from 1 not 0

TheVillageIdiot
A: 

You need nth-child(2), :nth-child(1) picks out title. Remember that :eq is 0-based, while :nth-child() is 1-based. Confusing!

Jim Ferrans