1

I am currently using Jquery to alter a 3rd party product and ran across this particular limitation...I think

Currently the checkbox ID and Name value have a ; in it see below

<input id="K48_h6;547ECE8D417501D623E11F94E0DF94FD" name="K48_1" value="h6;547ECE8D417501D623E11F94E0DF94FD" type="checkbox" style="cursor: pointer;">

I am trying to force Jquery to Check this box. This is the Jquery I have right now.

$("#K48_h6;547ECE8D417501D623E11F94E0DF94FD").prop("checked",true);

This is causing me to have a Object doesn't support this property or method.

Since this is a 3rd party product there is nothing I can do with the ;, The Jquery run post the server side code loading.

JuniorFlip
  • 417
  • 2
  • 7
  • 17
  • 2
    `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 (".").` http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html According to this, you've an invalid ID – Ejaz Apr 24 '13 at 21:49
  • of course you are aware than having ';' in ID attribute is not valid. Cannot you replace it? – A. Wolff Apr 24 '13 at 21:50
  • 1
    @Ejay in HTML5, IDs starting with number are valid now – A. Wolff Apr 24 '13 at 21:51
  • @roasted thanks for adding useful information to the page :) – Ejaz Apr 24 '13 at 21:53

3 Answers3

2

Alternatively to escaping, which is a fine solution, it seems this limitation doesn't exist when filtering for id as an attribute, like this:

$("input[id='K48_h6;547ECE8D417501D623E11F94E0DF94FD']").prop("checked",true);

See working demo

Nelson
  • 49,283
  • 8
  • 68
  • 81
1

Use native JS with document.getElementById() rather than just jQuery.

http://jsfiddle.net/droppedonjapan/ppFSh/1/

EDIT

If you need to have that element wrapped in jQuery, you can wrap it after capturing the element with getElementById().

Community
  • 1
  • 1
AlbertEngelB
  • 16,016
  • 15
  • 66
  • 93
  • 1
    +1 - it's good to always keep in mind that you're writing *JavaScript*, using the jQuery library, rather than a mysterious language called "jQuery". – user428517 Apr 24 '13 at 21:53
1

Have you considered just escaping (note the \\ before the ;) the invalid characters:

jQuery(function() {
    $('#K48_h6\\;547ECE8D417501D623E11F94E0DF94FD').prop('checked',true);
})

JS Fiddle demo.

Note that I don't particularly recommend trying to work around the use of invalid characters in your ids, I would seriously and quite strongly recommend changing whatever 3rd-party product you're using, for one that's capable of generating valid id properties/attributes.

David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • This will only work if you are manually entering the ID you're wanting. If you get that value from the DOM somewhere, you can get it to work doing `idVariable.replace(';', '/;')` – AlbertEngelB Apr 24 '13 at 21:55
  • Well, strictly speaking `idVariable.replace(/;/g,'\\\\;')`, surely? And if replacing is happening, then it'd be far easier to just *remove* the `;` characters, rather than escaping them. – David Thomas Apr 24 '13 at 21:56
  • Haha yeah derp. That'll be the last time I whip out `.replace()` code off the top of my head. :P – AlbertEngelB Apr 24 '13 at 21:57