user2669997 user2669997 - 1 year ago 64
Ajax Question

I want to get jquery to update checked box using ajax

I am trying to get my ajax json script to update my HTML checked box with the value from my data base but cannot get it to work. Any help please.

The JS is below.

success: function(data){
$('niamh').prop('checked', data.niamhswitch);
}, 2000);

The HTML checked box I want updated is below

<div class="niamhSwitch">
<label class="switch">
<input type="checkbox" name="niamh" id="switch" value="1">
<div class="slider round"></div>

I Know that the json is returning niamhswitch":"1" so I have the value 1 returning from my data base, but how do I get this to make the checked box become checked for value 1 and unchecked for value 0. I have tried doing it with the element selector for class and name but both do not work.

Answer Source

Your selector's wrong $('niamh') will look for an element whose tag name is 'niamh'.

You need either $('[name=niamh]') or $('#switch').

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download