1

I have a jQuery function to prettify radio buttons that works great except under one circumstance, when the name attribute of the button contains a bracketed array value.

This is for a shopping cart system that has selectable options via radio button, so the buttons look like so:

<input type="radio" name="option[218]" value="5">
<input type="radio" name="option[218]" value="6">
<input type="radio" name="option[218]" value="7">
...

The section of the jQuery script that's throwing the error looks like so:

$('input:radio[name=' + radio.attr('name') + ']')
  .not(':disabled')
  ...

Error:

Uncaught Error: Syntax error, unrecognized expression: input:radio[name=option[218]]

Obviously it's not expecting to find brackets [ ] in the name value.

How can I switch this up to allow those to be found and not error out?

2 Answers 2

2

You need to wrap the value inside double quotes " "

$('input:radio[name="' + radio.attr('name') + '"]')
// ---------------- ^      here and here       ^

So your value will be escaped properly and output as:

input:radio[name="option[218]"]
Sign up to request clarification or add additional context in comments.

1 Comment

Sometimes I feel like I should just take out my brain and play with it. Thanks Felix.
1

You can enclose the value in "" to escape the special characters

$('input:radio[name="' + radio.attr('name') + '"]')

1 Comment

Thanks Arun. Felix beat you to the punch. Appreciate your answer though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.