Does anyone know how to escape the square bracket character when setting a class name with jQuery?
Try the following:
$('#txtFirstname').addClass('test[someval]')
then
$('#txtFirstname').attr('class')
you'll see the class is there.
Now try
$('#txtFirstname').hasClass('test[someval]')
FAIL
The only reason I can think of is the square brackets.
And I need these for my jQuery validation you see.
What Does Bracket Mean? Brackets, or braces, are a syntactic construct in many programming languages. They take the forms of "[]", "()", "{}" or "<>." They are typically used to denote programming language constructs such as blocks, function calls or array subscripts. Brackets are also known as braces.
We can use a backslash to escape characters that have special meaning in regular expressions (e.g. \ will match an open bracket character).
Square brackets [] Square brackets are used for declaration of arrays and for selection of array elements. Curly brackets {} Curly brackets are used for collecting statements in blocks. For example, the definition of a class and of a method is placed between curly brackets.
A declarative tag is depicted by square ([ ]) brackets placed above the element it is used for. For example, attributes could be used to indicate whether a class is serializable, or which field in a database a particular property should be written to and so on... The attribute is OperationContract .
Escape with TWO (2) backslashes.
http://learn.jquery.com/using-jquery-core/faq/how-do-i-select-an-element-by-an-id-that-has-characters-used-in-css-notation/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With