Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape square brackets when assigning a class name to an element

Tags:

jquery

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.

like image 331
Duncan Avatar asked Sep 23 '09 13:09

Duncan


People also ask

What does square bracket mean in coding?

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.

How do you escape a square bracket?

We can use a backslash to escape characters that have special meaning in regular expressions (e.g. \ will match an open bracket character).

What does square brackets mean in Java?

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.

What is the use of square brackets in C#?

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 .


1 Answers

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/

like image 87
Duncan Avatar answered Sep 22 '22 11:09

Duncan