Having Issue on Wrapping and Unwrapping Un Selected Checkbox
I am trying to wrap each of disabled checkbox elements in a group into a div which enable users to click on the disabled items and be prompted if want to increase the limit or not?
As you can see I tried to wrap the checked elements with `.fake-check-wrap` and all not checked in `check-wrap-sapn` dives. This is because the jquery unwrap() will remove all first parent of the checkboxes
- $("input:checkbox").unwrap("<span class='check-wrap-sapn'></div>");
now I am totally lost here!
- <div class="container">
- <div class="well">
- <input type="checkbox" />
- <input type="checkbox" />
- <input type="checkbox" />
- <input type="checkbox" />
- <input type="checkbox" />
- <input type="checkbox" />
- <input type="checkbox" />
- <input type="checkbox" />
- <input type="checkbox" />
- <input type="checkbox" />
- <input type="checkbox" />
- <input type="checkbox" />
- <input type="checkbox" />
- <input type="checkbox" />
- <input type="checkbox" />
- </div>
- </div>
- var limit = 2;
- $("input:checkbox").on("change", function () {
- if ($("input:checkbox:checked").length <= limit) {
- var $this = $(this);
- if ($this.is(":checked")) {
- if ($this.parent().is(':not(.fake-check-wrap)')) {
- $this.wrap("<span class='fake-check-wrap'></div>");
- }
- } else {
- if ($this.parent().is('.fake-check-wrap')) {
- $this.unwrap("<div class='fake-check-wrap'></div>");
- }
- $("input:checkbox").not(":checked").attr("disabled", false);
-
- }
-
- }
- if ($("input:checkbox:checked").length >= limit) {
- $("input:checkbox").not(":checked").attr("disabled", true);
- $("input:checkbox").not(":checked").wrap("<span class='check-wrap-sapn'></div>");
- $("input:checkbox").not(":checked").after("<div class='check-wrap-mask'></div>");
- }
-
- });
can you please help me to gfix this? in a way if user selects 3 items the all other be disabled but the disabled wrap by
check-wrap-sapn' and if one of 2 selected be unchecked then unwrap only
check-wrap-sapn' from the unchecked checkboxes