I am doing a small project with Mysql as backend, Flask as framework. When I load the page, drop down list should be populated with values from database (onLoading() in my code.When I select "All" option from drop down list with checkboxes(I can select multiple), all the options in the list should be checked. when I am unchecking "All", all options should be unchecked. I tried this code. But somehow it is not working. console.log is printing "Outside" and "Inside" correctly when I am selecting 'All' but other options are not getting checked. May I know where the flaw is in my code.
HTML
<body onload=onLoading()>
<select id="nodes-select" multiple="multiple">
</select>
</body>
Javascript
<script type="text/javascript">
$('#nodes-select').change(function(){
console.log("Outside");
if($("#nodes-select option[value='-1']").is(':selected'))
{
console.log("Inside");
$('#nodes-select option').prop('selected', true);
$("#nodes-select option[value='-1']").prop('selected', false);
}
});
function onLoading()
{
alert("Loaded");
$.get("/nodes",function(data,status)
{
var tmp = data.output;
console.log(tmp);
$('#nodes-select').append($('<option>', {
value: -1,
text : "All"
}));
for(var i =0;i<tmp.length;i++)
{
console.log(tmp[i]);
$('#nodes-select').append($('<option>', {
value: tmp[i],
text : tmp[i]
}));
}
$('#nodes-select').multiselect('rebuild');
});
}
</script>