3

I'm writing javascript for a page that was built using a WYSIWYG. No, I cannot at present go in and change the IDs or really tweak the DOM.

This is the DOM element I'm trying to select (this was created by the WYSIWYG).

<td nowrap="true" align="left" id="vW2070;" name="vW2070;" style="padding-left: 2pt; padding-right: 2pt; border-left: 1pt solid black; cursor: auto; background-color: rgb(164, 194, 64); color: rgb(0, 0, 0);" class=" nAll selected" selected="1" sn="itemList">TOP 5</td>

Currently in my javascript the ID I'm trying to get is #vW2070;. For some reason document.getElementById("vW2070;") returns perfectly but $("#vW2070;") returns null. Why is this? I need to use the jQuery selector.

The function in question is ran on $(document).ready.

I've never seen anything like this.

Thank you, all.

EDIT: Without the semi-colon it doesn't pick it up either. The semi-colon is literally in the ID attribute on the element thanks to the WYSIWYG.

Jesse Atkinson
  • 8,386
  • 12
  • 38
  • 44

7 Answers7

7

You can escape the semicolon like this:

$("#vW2070\\;") //Use one backslash to escape the semicolon for the Sizzle parser and the other to escape the first backslash for the javascript parser, so two in total

or

$(document.getElementById("#vW2070;")).jquerystuff(...);

JSFiddle: http://jsfiddle.net/HTyQC/

Also, a relevant link on how versatile the id attribute is.

Dennis
  • 30,518
  • 9
  • 59
  • 75
  • While your solution *should* work, for some reason when I escape it, it still fails. I've realized now that document.getElementById("vW2070;") returns null as well. I have no idea why. This is insane... I'm going to mark this answer as correct. When I figure out what's wrong with my code and my crazy situation I'll post the solution here. – Jesse Atkinson Dec 22 '11 at 14:46
  • Again... trust me... I would've never used it. It's a tool called MicroStrategy which tracks analytics data. I was recently hired in and the site had already been built using their WYSIWYG tool. Phase 2 (next year) is for me to re-do the whole thing :) I can't wait. We're doing it this way now to meet a deadline. I couldn't exactly re-write the whole thing. – Jesse Atkinson Dec 22 '11 at 15:05
3

Since you can't edit your ID that has a semicolon, try escaping it in the selector, like so:

$("#vW2070\\;")

Escaping here is nothing more than adding the "\ \" before the semicolon.

Here's some more insight: What are valid values for the id attribute in HTML?

Community
  • 1
  • 1
motoxer4533
  • 2,658
  • 19
  • 34
1

Try removing the ; your shouldn't need it

$("#vW2070")
Dominic Green
  • 9,716
  • 4
  • 27
  • 34
1

Try removing the semi-colon $("#vW2070;") to $("#vW2070")

Are you not getting an error message? In chrome you should get Uncaught Error: Syntax error, unrecognized expression: ;

Richard
  • 4,038
  • 5
  • 30
  • 50
1

you should try escaping ";" by using "\"

change $("#vW2070;") to $("#vW2070\\;")

fiddle : http://jsfiddle.net/28cZu/

dku.rajkumar
  • 17,470
  • 7
  • 39
  • 57
1

If your element's id really does have a semi-colon in it, then technically its invalid.

Edit: The HTML5 spec doesn't appear to preclude any characters other than spaces.

You can just espace the semi-colon with a double backslash #vW2070\\;

Otherwise, just drop the semi-colon from your selector code.

Community
  • 1
  • 1
isNaN1247
  • 17,115
  • 12
  • 66
  • 114
0

It is jquery bug or we can say limitation in current jquery version. It will not allow ';' in ID.

Robin Michael Poothurai
  • 4,794
  • 7
  • 21
  • 36