programiss
programiss

Reputation: 255

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!!

Upvotes: 0

Views: 172

Answers (1)

Steven Lacks
Steven Lacks

Reputation: 904

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/

Upvotes: 1

Related Questions