Sailing Judo
Sailing Judo

Reputation: 11243

How to get an element with jquery that has an illegal character in the id?

A third-party control creates an element with an id that looks like:

 aspxgv_1803_0_cell1_9_Attribute1^ubd

I need to get the value of this element but jquery seems to have issues with the illegal "^" in the id.

 var fieldId = 'aspxgv_1803_0_cell1_9_Attribute1^ubd';
 var fieldValue = $('#' + fieldId).val();  // <-------- function stops

How can I get the value of this element?

Upvotes: 2

Views: 119

Answers (2)

Nimphious
Nimphious

Reputation: 5039

If you want a jQuery object of a DOM element you can always just pass the element to the $ function rather than a selector.

var fieldId = 'aspxgv_1803_0_cell1_9_Attribute1^ubd';
var fieldValue = $(document.getElementById(fieldId)).val();

Here's an example using this code to return the html of the jQuery object. http://jsfiddle.net/AZgwm/

Upvotes: 3

Ram
Ram

Reputation: 144659

You can escape the character.

var fieldValue = $('#aspxgv_1803_0_cell1_9_Attribute1\\^ubd').val();

http://jsfiddle.net/82AFA/

Upvotes: 4

Related Questions