2

I want to rewrite url parameters values on checkbox click, similar to LinkedIn Advance search.

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
  <script type='text/javascript'>
  
  $(document).ready(function () {
$('input[type=checkbox]').click(function (e) {
var seasoning = jQuery.map($(':checkbox[id=seasoning\\[\\]]:checked'), function (n, i) {return n.value;}).join(',');

window.location ='example.com?seasoning='+seasoning;
 
});

});
</script>
<h3>Filter recepies:</h3>

<div>
<p>Select vegetables</p>
<label><input type="checkbox" id="vegetables[]" value="potato"> Potato</label><br>
<label><input type="checkbox" id="vegetables[]" value="onion"> Onion</label><br>
<label><input type="checkbox" id="vegetables[]" value="tomato"> Tomato</label><br>
</div>

<div>
<p>Select seasoning</p>
<label><input type="checkbox" id="seasoning[]" value="salt"> Salt</label><br>
<label><input type="checkbox" id="seasoning[]" value="pepper"> Pepper</label><br>
<label><input type="checkbox" id="seasoning[]" value="chilli"> Chilli Flakes</label><br>
</div>

My Desired Result

Click1: when I click Potato from vegitable my URL should look like
example.com?vegitables=potato

Click2: when I click Onion from vegitable my URL should look like
example.com?vegitables=potato,onion

Click3: when I click salt from seasoning my URL should look like
example.com?vegitables=potato,onion&seasoning=salt

Click4: when I click pepper from seasoning my URL should look like
example.com?vegitables=potato,onion&seasoning=salt,pepper

1
  • Check the answer i have posted, exactly what you need. Commented Jul 8, 2017 at 12:07

1 Answer 1

1

Here is what you can do

$(document).ready(function () {
  $('input[type=checkbox]').click(function (e) {
  var seasoning = '', tempArray = [];
  $('input[name="vegetables[]"]:checked').each(function(){
      tempArray.push($(this).val());
  })
  if(tempArray.length !== 0){
     seasoning+='vegetables='+tempArray.toString();
     tempArray = [];
  }
  
  $('input[name="seasoning[]"]:checked').each(function(){
      tempArray.push($(this).val());
  })
  if(tempArray.length !== 0){
     seasoning+='&seasoning='+tempArray.toString();
  }
 
 // window.location ='example.com?seasoning='+seasoning;
 console.log('example.com?'+seasoning);

  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<p>Select vegetables</p>
<label><input type="checkbox" name="vegetables[]" value="potato"> Potato</label><br>
<label><input type="checkbox" name="vegetables[]" value="onion"> Onion</label><br>
<label><input type="checkbox" name="vegetables[]" value="tomato"> Tomato</label><br>
</div>

<div>
<p>Select seasoning</p>
<label><input type="checkbox" name="seasoning[]" value="salt"> Salt</label><br>
<label><input type="checkbox" name="seasoning[]" value="pepper"> Pepper</label><br>
<label><input type="checkbox" name="seasoning[]" value="chilli"> Chilli Flakes</label><br>
</div>

For simplicity and further edit here is the link to working JSFIDDLE

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

1 Comment

this is working like a charm for the 2 parameter (vegetables,seasoning)! i am getting wrong value if i add 3rd parameter (vegetables,seasoning,param3) js code for param3 $('input[name="param3[]"]:checked').each(function(){ tempArray.push($(this).val()); }) if(tempArray.length !== 0){ seasoning+='&param3='+tempArray.toString(); }

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.