views:

74

answers:

7

Hi all:

I have the following DOM structure:

/*:DOC += <div id='testDiv' class='testDivClass'><div id='innerTestDiv'></div></div><span id='testSpan' class='testSpanClass'><span id='innerTestSpan'></span></span>

Now I tried to run jQuery select against them as follow. The first one returned alright, but the second one failed:

// works
var result = $('#testDiv')[0];
alert(result.id);

// failed: id is null or not an object
var result2 = $('#testSpan')[0];
alert(result2.id);

I tried selecting id instead of class and got the same results.

My question is: how can I get the second select to work? Is there some sort of invisible iterator/pointer in jQuery which I need to reset to the beginning of the DOM before the second select?

Thanks.

EDIT: Ok this is the official "does not work" version. testDiv matched, but testSpan did not, hence I got an error saying id is null or not an object error in the second alert.

UPDATE: I did a test by swapping testDiv and testSpan in the html. Now BOTH select failed.

UPDATE2: I have changed the html back to what it used to look like. I'm using JsTestDriver to write up the test, but it is actually not calling anything at the moment. The actual html looks messier than this (more nested tags). I'm trying to get this simplified version to work first. It appears that jQuery was able to get into the first select, whether it'll be span or div, but couldnt get out of it to do the second select. I've replaced jQuery.js and jsTestDriver.jar to no avail.

Thanks.

A: 

Use "#" to select by id, use "." to select by class...

James Kolpack
Too many typos in my question... its updated now.
BeraCim
+1  A: 

The .className selector matches by class, not ID.
Therefore, $(span.testSpan) won't match any elements.
You need to change it to $('span.testSpanClass') ot $(span#testSpan') (using the #id selector, which matches ID).

For more information, read the documentation.

SLaks
Sorry I had too many typos in my question... its been updated now.
BeraCim
+2  A: 

There is no reason that that code wouldn't work.
Can you post a demo?

SLaks
+1  A: 

I don't know why, but for me your code worked well.

I added $(document).ready(function() { before that code, and when I opened the test page, the alert box showed up perfectly, both of them! I don't know when do you want this alert box showed, but if it is when visitor open the page, just add that code. Otherwise, add

function objectid() {
var result = $('#testDiv')[0];
alert(result.id);
var result2 = $('#testSpan')[0];
alert(result2.id);
}

That code worked well for me, too.

PS: Sorry if you don't understand my bad english.

Jim
+1  A: 

More than likely, there is something else wrong with the HTML you're actually using. Since you're posting only a tiny bit of the html, we can't actually test your problem. Post the entire page, or at least the smallest piece of it that actually has the problem when you run your test.

John Fisher
I agree; that seems to be the more probable reason. It would help if it would be possible to see the complete jQuery code, and HTML.
kiamlaluno
+1  A: 

I tested the jQuery code you reported on JS Bin, and the code worked fine. As the code is very basic, I don't think the problem is caused by the version of jQuery used.

kiamlaluno
A: 

What I ended up doing is wrapping the entire html with a div or span tag. I found that jQuery could not get out of a div/span tag once it gets into one (in my above example), so I just make it to go into a div/span tag once.

Not sure whether this is a patch or ugly fix, but it solved my problem for now.

Thanks for all the help!

BeraCim