I have used the below JavaScript code to get the parameter from the URL and checked the input field. It is working fine if I don't use name="community[]"
third bracket in my input field. If I used third bracket in JavaScript code then it is not working.
html
<input type="checkbox" id="type" name="community[]" value="<?php echo $comvalue;?>">
JavaScript
var i = document.location.href.lastIndexOf('?');
document.location.href.substr(i+1).replace(/community=/g,'').split('&');
$('input[name="community"]').prop('checked',function(){
return $.inArray(this.value,types) !== -1;
});
The issue is because you're missing the []
from the name
attribute in your selector:
var types = ['A', 'C'];
$('input[name="community[]"]').prop('checked', function() {
return $.inArray(this.value, types) !== -1;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="type" name="community[]" value="A">
<input type="checkbox" id="type" name="community[]" value="B">
<input type="checkbox" id="type" name="community[]" value="C">
</div>