RealityDysfunction
RealityDysfunction

Reputation: 2639

add escape characters to jquery variable on the fly

Is there a way to automatically escape characters in a jQuery variable? My variable looks like this:

var $name = $(this).prev().attr('name');

Things get hairy now because the name has square brackets. So when I try to do this:

$('input:radio[name='+$name+']').next().addClass('so_pretty');

javascript throws a fit because it gets confused from all the brackets.

Anyway to add those escape slashes on the fly?

Thanks, Leo

Upvotes: 3

Views: 1619

Answers (1)

PSL
PSL

Reputation: 123739

Wrap it in quotes, so that meta char issue doesn't happen since [, ] are metachars for a jquery selector.

$('input:radio[name="'+$name+'"]').next().addClass('so_pretty');
                    ^__       ^___

Upvotes: 6

Related Questions