getElementByid en Javascript cuando la ID del elemento contiene el signo de dólar

getElementById() does not appear to find the element when the ID contains a dollar sign. Is there a way around this in pure Javascript? I have no control over the element ID's as these are generated by a "page builder" and I cannot use jQuery in this application.

Ejemplo:

<div id="win0divFRED$0">

var orig = document.getElementById("win0divFRED$0");

Muchas gracias,
John

preguntado el 31 de julio de 12 a las 11:07

funciona bien jsfiddle.net/XKn5X -

Works fine for me - there should be no problem. -

Works fine for me too : jsfiddle.net/akhurshid/Chtkj -

may depend on the doctype i guess, try the new one:<!doctype html> -

Many thanks for such fast responses. Alas, doesn't work in my world and the answer from Christoph regarding the doctype is probably the reason why. I can't change from HTML4.01 as the pages are generated by an application I have no control over. The pages are generated with a mixture of tokens, some contain dollar signs and some do not. getElementByid finds the non-dollar elements fine, but not those containing a dollar sign in the ID. -

1 Respuestas

Since the HTML4.01 spec is pretty restrictive

Los tokens de ID y NAME deben comenzar con una letra ([A-Za-z]) y pueden ir seguidos de cualquier número de letras, dígitos ([0-9]), guiones ("-"), guiones bajos ("_") , dos puntos (":") y puntos (".").

your id would be invalid according to 4.01 spec.
In contrast to HTML5:

El valor debe ser único entre todos los ID en el subárbol de inicio del elemento y debe contener al menos un carácter. El valor no debe contener espacios.

I think it might be a problem with the doctype. Try:

<!doctype html>

Respondido 31 Jul 12, 11:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.