2

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;
});
0

1 Answer 1

5

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">

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer, but I have already tried this code. Its not working.
In which case you need to check your console for errors elsewhere in your code. As you can see from the snippet I added to this answer, it works absolutely fine.
Yes your code is working. But I have a problem? How to get community parameters to array? http://test/apartments/?price-min=200&price-max=2356&bedroom=3&bathroom=2&ap_features%5B%5D=washer+%26+dryer+connections&ap_features%5B%5D=dishwasher&community%5B%5D=business_center&community%5B%5D=clubhouse&community%5B%5D=covered_parking&submit=SEARCH

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.