isSelected
Will return true or false whether or not an <option>
or <input>
element of type
checkbox or radio is currently selected.
Usage
$(selector).isSelected()
Examples
- Asynchronous Mode
- Synchronous Mode
index.html
<select name="selectbox" id="selectbox">
<option value="John Doe">John Doe</option>
<option value="Layla Terry" selected="selected">Layla Terry</option>
<option value="Bill Gilbert">Bill Gilbert"</option>
</select>
isSelected.js
it('should detect if an element is selected', async () => {
let element = await $('[value="Layla Terry"]');
console.log(await element.isSelected()); // outputs: true
element = await $('[value="Bill Gilbert"]')
console.log(await element.isSelected()); // outputs: false
});
index.html
<select name="selectbox" id="selectbox">
<option value="John Doe">John Doe</option>
<option value="Layla Terry" selected="selected">Layla Terry</option>
<option value="Bill Gilbert">Bill Gilbert"</option>
</select>
isSelected.js
it('should detect if an element is selected', () => {
let element = $('[value="Layla Terry"]');
console.log(element.isSelected()); // outputs: true
element = $('[value="Bill Gilbert"]')
console.log(element.isSelected()); // outputs: false
});
caution
Synchronous Mode will depcrecated with Node.js v16. With an update to the underlying Chromium version it became technically impossible to provide the same synchronous behavior. We recommend to start transition to asynchronous command execution. For more information, see our RFC.