Mark Eriksson
Mark Eriksson

Reputation: 1475

jQuery issue with HTML indexes

In my HTML I have a textarea element with the ID of Employment[Duties].

When I try to access this element through jQuery, it completely ignores it.

I think this is because of the whole input[type='text'] selector that jQuery has where you can select specific items within the [] brackets.

Is there any way at all to get around this?

Upvotes: 3

Views: 61

Answers (2)

Sergio
Sergio

Reputation: 28837

Use this to escape the square brackets:

$('#Employment\\[Duties\\]')

EDIT: This problem doesn't normally apply in plain javascript, so that opens other options for you:

  • you can use plain javascript: document.getElementById('Employment[Duties]')

  • or you can define a variable with var emp = document.getElementById('Employment[Duties]'); and then wrap it with jQuery $(emp) and use jQuery selectors on it.

Upvotes: 6

Mike Brant
Mike Brant

Reputation: 71384

You can solve this problem by not using the invalid [] in your id's.

Note: This is allowable by HTML5 draft specification, but I would not rely on this working across all browsers (and then only for HTML5).

Upvotes: 2

Related Questions