Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery selector for radio button group in which the name attributes value has array notation

I want to use jquery selector for radio button group with array notation

I have radio buttons like this

<input name="show[]" type="radio" value="1" /> Show<br>
<input name="show[]" type="radio" value="0" /> Hide<br>

I want to use jquery selector for the above and tried the following

$("input:radio[name=show[]]").click(function(){
    alert(this.value)
})

which is not working

I know we can give like show instead of show[] for the name attribute of radio button

yet IE had problems so by giving with array notation worked in all browsers.

Now i want to give like what i had done and is that possible or is it a different syntax to include array notation?

like image 338
Jayapal Chandran Avatar asked Dec 12 '22 21:12

Jayapal Chandran


1 Answers

You need to escape the square brackets in the selector:

$("input:radio[name=show\\[\\]]").click(function(){
    alert(this.value)
})

As mentioned By James Allardice, you can also put quotes around the attribute itself to stop the ambiguity:

$("input:radio[name='show[]']").click(function(){
    alert(this.value)
})

Better yet, use a class.

like image 60
Rory McCrossan Avatar answered Dec 22 '22 01:12

Rory McCrossan