user2669997 user2669997 - 2 months ago 11
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.



setInterval(function(){
$.ajax({
url:"databaseQuiry.php",
dataType:"json",
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>
</label></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

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

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

Comments