javascript option
/* assuming we have the following HTML
<select id="s">
<option>First</option>
<option>Second</option>
<option>Third</option>
</select>
*/
var s = document.getElementById('s');
var options = [ 'zero', 'one', 'two' ];
options.forEach(function(element, key) {
if (element == 'zero') {
s[s.options.length] = new Option(element, s.options.length, false, false);
}
if (element == 'one') {
s[s.options.length] = new Option(element, s.options.length, true, false); // Will add the "selected" attribute
}
if (element == 'two') {
s[s.options.length] = new Option(element, s.options.length, false, true); // Just will be selected in "view"
}
});
/* Result
<select id="s">
<option value="0">zero</option>
<option value="1" selected="">one</option>
<option value="2">two</option> // User will see this as 'selected'
</select>
*/
Also in JavaScript:
- how to write a program that displays a message “It’s Fun day” if it's Saturday or Sunday today in javascript
- python convert requests response to json
- javascript get duplicates in array
- document is not defined javascript in nuxt js
- load +main.js with system.import
- javascript remove the current tr with click
- required false jquery
- electron remove cors
- Lazy Loading Routes vue
- random array javascript
- pass header in ajax
- turn number into array javascript
- create array javascript fill
- setting property to method in child class javascript
- javascript array add
- make list in javascript
- s3 list objects in folder node js
- how to send message to specific client using ws node js
- display json data in html table using javascript dynamically
- jquery move element to another without losing events
- jquery show hide based on data attribute
- check if object has method javascript
- how to get csrf token in javascript
- nknown key passed via urlObject into url.format: searchParams