简体   繁体   中英

How can I change the property of my css attributes?

I have the following css code :

input:checked + .selectablelabel .check {
  visibility: hidden;
}

And I would like to modify using javascript the property of the attribute visibility and to set to visible using javascript.

I tried this :

$(document).on('click', '#selectlabelall', function () {
document.getElementsByClassName("input:checked + .selectablelabel .check").visibility = "visible";
});

But without any effects.

Could you help me please ?

Thank you very much !

Hey the way you are using is incorrect! Use this instead :

  $(document).on('click', '#selectlabelall', function () {
  let elements = 
 document.getElementsByClassName("input:checked + 
 .selectablelabel .check");

 for(element of elements) {}
      element.style.visibility ="visible":
  }
  );
$(document).on('click', '#selectlabelall', function () {
    var ele = document.getElementsByClassName("selectablelabel check")
    var i = 0;
    while(i < ele.length) {
        ele[i].style.visibility = 'hidden'
        i++
    }
});

Adding the style attribute should solve your problem. Also try using the jQuery way to select the element since you are already using the library

I'm not quite sure what your intent is for the full functionality, but here are my assumptions for your specs:

  1. hide the label of any checked checkbox
  2. show label of any unchecked checkbox
  3. if, the "selectall" checkbox is checked, you should show the labels of the other checkboxes regardless of whether they are checked or not

If that's all true above, then you could handle all this in CSS:

 input:checked + .selectablelabel .check { visibility: hidden; } #selectlabelall:checked ~ .selectablelabel .check { visibility: visible; }
 <input type="checkbox" id="selectlabelall"> <input type="checkbox"> <label class="selectablelabel"> <span class="check">one</span> </label> <input type="checkbox"> <label class="selectablelabel"> <span class="check">two</span> </label> <input type="checkbox"> <label class="selectablelabel"> <span class="check">three</span> </label>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM