3

I am creating html element ids by escaping text phrases, as follows:

var elementid=escape('some term entered by user');

As a result, I have a span with an id as follows:

<span class="radio" id="selectTimescalesOne%20hello%20there" style="background-position: 0px 0px; "></span>

If I attempt this:

$('#selectTimescalesOne%20hello%20there').html('some new stuff');

Then the span does not get updated. Is there something wrong with using escaped strings as ids?

Demo: http://jsfiddle.net/uSwEJ/

thecodeparadox
  • 84,459
  • 21
  • 136
  • 161
Journeyman
  • 9,671
  • 15
  • 76
  • 124
  • 1
    possible duplicate of [What are valid values for the id attribute in HTML?](http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html) – Pekka Sep 08 '11 at 19:03
  • Unfortunately, it looks like you will need to replace the spaces by something else, e.g. underscore `_`. – Pekka Sep 08 '11 at 19:03

5 Answers5

7

% is not valid in an id. You are limited to alphanumeric characters, _, - ':' and ., although it is wise not to use the latter three, because they cannot be used in CSS selectors.

I really wouldn't know why you want a user to input your id. Seems like bad design. If you do want to do this, just remove any invalid character or replace it with an underscore.

GolezTrol
  • 111,943
  • 16
  • 178
  • 202
1

Try replacing whitespace (using a regex) with an underscore.

Daniel A. White
  • 181,601
  • 45
  • 354
  • 430
1

Try removing the spaces in the text.

Praveen
  • 1,461
  • 16
  • 25
0

According to the W3C specification, you can't use a '%' in your ID.

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")

Babak Naffas
  • 11,988
  • 3
  • 35
  • 49
0

In your case, I think it would be best to strip any special characters and replace all white spaces with dashes or underscores.

Madara's Ghost
  • 165,920
  • 50
  • 255
  • 304