Frank Fan Frank Fan - 7 months ago 10
Javascript Question

if any of these checkbox with same name is changed, then do somthing

I would like to create several checkboxes. Then if any of the checkboxes's check status is changed, then do something like print out a word in a div.

The code in the demo doesn't work but it is the idea.
demo: https://jsfiddle.net/slfan/ut8v5xzk/

HTML:

<input type="checkbox" name="a" value=1>Options1<br>
<input type="checkbox" name="a" value=2>Options2<br>
<input type="checkbox" name="a" value=3>Options3<br>
<input type="checkbox" name="a" value=4>Options4<br>
<input type="checkbox" name="a" value=5>Options5<br>
<p id="demo" style="background-color:yellow;">Not Succeed yet.</p>


Javascript:

$("input[name=a]").on("change",function(){
$("#demo").text("success!")})

Answer

You forgot to put jQuery in the fiddle:

Here is one where it works:

https://jsfiddle.net/cu7tn64o/7/

Comments