0

I am using jQuery 1.8.0 as part of a WordPress plugin and storing mouse click x/y coordinates and keeping the node information including the parent path information (similar to Getting a jQuery selector for an element).

After some experimentation I noticed that jQuery could not read some of the selectors I had stored. Some examples:

html#:eq(0) -> the '#' is not necessary but jQuery evaluates it with or without the '#'!
body#:eq(0) -> jQuery cannot evaluate this, it aborts/stops at this line (different behavior to html tag)

div#:eq(1) -> jQuery evaluates this (should abort?)
div:eq(1) -> jQuery aborts/stops at this line (should work?)

Is this a bug (if it is I will report it to jQuery)? My understanding from comments made to this question is that the '#' should be present if and only if there is an id for the element but this does not seem to be how jQuery is behaving.

Community
  • 1
  • 1
  • I'm not understanding the question? As in "quirks" you mean bugs? – mekwall Oct 16 '12 at 11:47
  • "when does a hash (#) have to be included even when an id tag is not present and when must it not be used?" Your first and second parts answer each other... – BoltClock Oct 16 '12 at 11:48
  • Can you test with another version. It seems that it works with 1.7.1 – Maxim Krizhanovsky Oct 16 '12 at 11:50
  • And what do your examples have to do with your question? It's fairly obvious that none of those are IDs and you should not use `#` in any of those selectors. If you're trying to report a bug, do it in their bug tracker. – BoltClock Oct 16 '12 at 11:55
  • I guess my code for generating the path is not as good as it could be as it is adding a '#' in some cases where it shouldn't, in some cases jQuery is forgiving and in others it is not. My main point is why does jQuery require a # for 'div#:eq(1)' even though this element does not have an id? Why shouldn't 'div:eq(1)' also work? Why does 'body#:eq(0)' hang but 'html#:eq(0)' does not, isn't the '#' redundant in both? – Byron Stuart Oct 16 '12 at 12:26
  • @MarcusEkwall by quirks I simply mean why does jQuery ignore a redundant '#' in some cases and in other cases insist it be there even though there is no id for that element? – Byron Stuart Oct 16 '12 at 12:31
  • @BoltClock it would seem in jQuery 1.8.0 that it requires div selectors to have a hash whether the div has an id or not. So you're saying this is actually a bug and not just a quirk? – Byron Stuart Oct 16 '12 at 12:36
  • What do you mean when you say jQuery "hangs"? I'm having a hard time reproducing the issues mentioned. – BoltClock Oct 16 '12 at 12:37
  • I am using jQuery as part of a WordPress plugin. To check if a selector is still present on the page I use the code 'if (jQuery(reference).length == 1)' What I found for something like the 'div:eq(1)' example is that the javascript does not get to the next line of code, so it's throwing an exception or something, that's what I mean by 'hangs'. Sorry perhaps 'hangs' was not the best choice of word. – Byron Stuart Oct 16 '12 at 13:03
  • Here's an example of a stored selector that my testing showed jQuery is happy to evaluate: 'html:eq(0) > body:eq(0) > div#colorbox:eq(0) > div#cboxwrapper:eq(0) > div#:eq(1) > div#cboxcontent:eq(0) > div.hover:eq(8)' If I remove the hash from the 3rd last div the script aborts on the jQuery(reference).length statement. From what everyone is saying that '#' should not be there but the code doesn't work unless it is. – Byron Stuart Oct 16 '12 at 13:15

1 Answers1

2

The hash is part of an id CSS selector, so it has to be included when you are targeting elements based on some id and it must not be included in all other situations.

All of your examples fall into the latter category.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • I don't even see any relation that the examples have to the question being asked. – BoltClock Oct 16 '12 at 11:56
  • The example of a div I gave is for an element that does not have an id e.g.
    blah etc
    . Why does this selector need a hash at all, e.g. div#:eq(1). If I do not include the # jQuery hangs.
    – Byron Stuart Oct 16 '12 at 12:29
  • @ByronStuart: The selector doesn't need a hash. It is possible that your code has a bug or runs terribly slow for some reason; including a hash causes an exception and halts script execution so you don't see that. Did you look at your browser's JS console? – Jon Oct 16 '12 at 12:46
  • @Jon I understand your logic but it would seem that the exact opposite is happening. If I don't include a '#' the javascript never gets to the next line of code. If I do include the '#' it evaluates it and continues. Perhaps this is a bug in 1.8.0? – Byron Stuart Oct 16 '12 at 13:09