0

I struggled with strange case. 1. The same page is opened and debugged in Chrome and Firefox. 2. Code is:

$("ul[id^='_'").each(function(){
         // some actions which work good in Chrome
});

3. While debugging I figured out that problem is in $("ul[id^='_'") 4. Trying to execute this in Chrome console I get:

enter image description here

that is OK and I expect. However the same code in Firefox 25 returns me the follow:

enter image description here

That is empty object, which obviously not. So what's difference between this code in two browsers? I suspect that problem might be in the cyrillic value of id-attribute, but it successfully works and IE and Chrome , and falls in Firefox and Mozilla ?

Please, help me to fix .

UPD: http://jsfiddle.net/jeston/WEm78/

Eugene Shmorgun
  • 2,083
  • 12
  • 42
  • 67
  • 1
    can you provide a fiddle? – reyaner Nov 11 '13 at 14:35
  • 1
    i think that id field cannot start with anything else other than letter. Try to rename your ul id field to something else (not start with symbol/letter) and check again your 2 browsers – andrew Nov 11 '13 at 14:36
  • You made a typo : `ul[id^='_']` Seems chrome automatically fixes this for you. – nkmol Nov 11 '13 at 15:02

3 Answers3

3

is that maybe wrong:

$("ul[id^='_']").each(function(){
         console.log(this)
});

//ul[id^='_'] <----

reyaner
  • 2,799
  • 1
  • 12
  • 17
1

An alternative solution

var $result = $('ul').filter(function(){
    return this.id.charAt(0) === '_';
});

console.log($result);

http://jsfiddle.net/2enJ3/

Johan
  • 35,120
  • 54
  • 178
  • 293
0

There is a good answer as to what to use for an ID.

In short, issue is a HTML ID field can start only with a letter, no numbers or special characters.

For example, these are valid IDs

hello
hello_1
hello_world_1
dropdown

These aren't

1_dropdown
$34_element
Community
  • 1
  • 1
Jason
  • 11,263
  • 21
  • 87
  • 181
  • A id can start with a underscore, so that wasn't the problem. – nkmol Nov 12 '13 at 07:29
  • actually the source you quote is from an old answer, referring to a [html4 spec](http://www.w3.org/TR/html401/types.html#type-name), it isn't true in [html5](http://www.w3.org/TR/html5/dom.html#the-id-attribute) – Enoah Netzach Mar 25 '14 at 15:22