1

This is similar to a previous question I asked but I'm not sure if my intention was clear enough. So I have made a jsfiddle and provide more details of what I'm looking to achieve.

I'm trying to build a hash which I can then send via an ajax request to perform a query, the hash would look like this category = { "gender": '', "styles": [] };

There are a few things I would like to happen:

1) A user can only select Mens or Womens at any one time, causing the other option to become unclickable (the clicked option should always be in the JS object).

2) if a user has Mens selected already and then clicks on the same option then that option becomes unchecked and the womens option then becomes clickable and vice versa.

3) When a type (so Mountain Bike etc) is selected it as added to the styles array, and when that type is unchecked it is then removed.

With my jsfiddle the li class active is not being updated (even though the parameter class appears) and I can't seem to grab the closest label to enable the switch statement to work.

1 Answer 1

2

This can be made a lot simpler than you are attempting to.

Firstly, to solve points 1 and 2 use a radio input - you get that behaviour by default. To meet point 3 you can use map() to create an array of the checked options. To make this simpler you can add class attributes on the inputs to make their identification much easier.

Also note that your HTML as it stands is invalid as you have not given a name attribute to any of your input elements and you cannot have an input inside an a element.

Finally, you should always hook to the change event on radio and checkbox elements to cater for people who navigate the web via their keyboard.

With all that in mind, here's a much simpler working example:

var category = {
  "gender": '',
  "styles": []
};

$(".multiselect-container input").on('change', function(event) {
  category.gender = $('.gender:checked').val();
  category.styles = $('.styles:checked').map(function() { return this.value; }).get();
  console.log(category);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="multiselect-container dropdown-menu">
  <li class="multiselect-item multiselect-group genders">
    <label><b>Gender</b>
    </label>
  </li>
  <li>
    <label class="checkbox">
      <input type="radio" value="Mens" name="gender" class="gender" />Mens
    </label>
  </li>
  <li>
    <label class="checkbox">
      <input type="radio" value="Womens" name="gender" class="gender" />Womens
    </label>
  </li>

  <li class="multiselect-item multiselect-group types">
    <label><b>Type</b>
    </label>
  </li>
  <li>
    <label class="checkbox">
      <input type="checkbox" value="Mountain Bike" class="styles" name="styles" />Mountain Bike
    </label>
  </li>
  <li>
    <label class="checkbox">
      <input type="checkbox" value="Hybrid" class="styles" name="styles" />Hybrid
    </label>
  </li>
  <li>
    <label class="checkbox">
      <input type="checkbox" value="Road" class="styles" name="styles" />Road
    </label>
  </li>
</ul>

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

8 Comments

wow, that is ridiculously simpler, i may have been constraining myself then as the html that is generated is a result of github.com/davidstutz/bootstrap-multiselect i will see if i can change the html then to use radio buttons instead, thank you so much for pointing all this out
No problem, glad to help. Personally I would find another plugin to use - I can see several issues with that one already.
any suggestions please ? and what issues do you see ?
Sorry, none off the top of my head. I'd have thought a quick google should find some though.
Ok but what problems do you see, just so I can not have the same issues with another plugin, thanks
|

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.