views:

164

answers:

7

I have a complex html-Form and am using jQ for some of the work there. And spent the best part of this afternoon trying to understand a problem which I was able to trace back to the point where jQuery failed to find an existing control. The command $("#FormView1_CopaBOM973row%18%_dkF").width(); returned null. First I thought about an error in the width()-implementation and tried $("#FormView1_CopaBOM973row%18%_dkF").attr("id"); - which did not return anything and its length was 0.

Im doing this in Firefox 3.5.5, have used Firebug's DOM-Explorer to verify the thing was there. And finally, the ultimate test worked: $(document.getElementById("FormView1_CopaBOM973row%18%_dkF")).attr("id") returned the expected result.

So, my only idea would be that any "suspicious" characters would need escaping, but the doc doesn't mention underscore or percent as "suspicous". So, where's my fault?

And pls. don't ask for more "code", I don't think this issue would need any more...

A: 

try $("#FormView1_CopaBOM973row%18%_dkF")[0].id;

Josh Pearce
A: 

'%' is not a valid character for an id attribute so probably jQuery doesn't like that.

Matthew
A: 

Escaping the percent signs seems to work.

$("#FormView1_CopaBOM973row\\%18\\%_dkF")
bdukes
+1  A: 

It appears that the list of special characters at the bottom of this page is incomplete. Try escaping your percent signs:

$("#FormView1_CopaBOM973row\\%18\\%_dkF")
schinazi
A: 

Those percents % are apparently illegal in CSS selectors (apparently because I can't find them in w3 spec, or it must be an unrevealed jQuery bug), but not in HTML attribues. Escaping them does the work though:

$("#FormView1_CopaBOM973row\\%18\\%_dkF").width();
BalusC
A: 

I think its because of special characters you have in your ID.

Try:

$("[id=FormView1_CopaBOM973row%18%_dkF]").width();
Technowise
+2  A: 

As Matthew said, special characters are not allowed on the ID attribute. This is not a jQuery problem. Here is the complete reference.

Josh
Thanks Josh - this makes it clear that I shouldn't be using % in the ID, I wasn't aware of that.
MBaas