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
  • 86,271
  • 21
  • 138
  • 164
Journeyman
  • 10,011
  • 16
  • 81
  • 129
  • 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
  • 114,394
  • 18
  • 182
  • 210
1

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

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
1

Try removing the spaces in the text.

Praveen
  • 1,449
  • 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
  • 12,395
  • 3
  • 34
  • 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
  • 172,118
  • 50
  • 264
  • 308