简体   繁体   中英

Javascript one liner to make sure checkbox is checked?

I am working with selenium ide and was wondering if there is a one line that can make sure a checkbox is checked.

or any other selenium javascript commands you've used in the past would be greatly appreciated.

Thanks all!!

If you want to return the value (read whether true or false): just add ".checked" to the end of the object that represents the checkbox, let's pretend our checkbox has an id="checkBox":

checkBox.checked  // returns true or false.

If you want to set the checked attribute to checked

checkBox.checked = true;

Looking at the link in the comment, it looks like you're using query selector. Unlike jQuery there's no built in each on the array like list returned by the querySelectorAll method...

In one line, it is messy...

To get:

Array.prototype.forEach.call(document.querySelectorAll('input[type="checkbox"]'), function(checkbox){return checkbox.checked; });

To set

Array.prototype.forEach.call(document.querySelectorAll('input[type="checkbox"]'), function(checkbox){checkbox.checked = true; });

edit: Added return keyword to the "get" version.

edit2: a jsfiddle: http://jsfiddle.net/snlacks/pLgjx4xa/

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