javascript - How can I let a user choose EITHER one option or many of the other ones? -


i have bunch of options in form user select. way options should work follows:

  • either user selects 'all categories' option.
  • or user can select 1 or more of other options.

so want clicking of 'all categories' option remove selection on other options, , want selection of options other 'all categories' unselect 'all categories' option. currently, of these options checkboxes, i'm open changing them.

i'm guessing i'll need use sort of jquery/javascript behavior. ideas?

just create simple event handler .change() event of checkboxes , set 'checked' property:

<html>  <form>     <p><input type="checkbox" name="option[]" class="selectall"> select all</p>     <p><input type="checkbox" name="option[]" class="option"> option 1</p>     <p><input type="checkbox" name="option[]" class="option"> option 2</p>     <p><input type="checkbox" name="option[]" class="option"> option 3</p>      <button type="submit">submit</button> </form>  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>  <script>     // event handler when item 'selectall' changed     $(".selectall").change(function(){         // set 'checked' property of items 'option' class         // have same value 'selectall' checkbox         $(".option").prop('checked', $(this).prop("checked"));     }); </script>  </html> 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -