make select option selected javascript
document.getElementById('personlist').getElementsByTagName('option')[11].selected = 'selected'
<form name="AddAndEdit">
<select name="list" id="personlist">
<option value="P1">Person1</option>
<option value="P2">Person2</option>
<option value="P3">Person3</option>
</select>
</form>(function() {
// get references to select list and display text box
var sel = document.getElementById('scripts');
var el = document.getElementById('display');
function getSelectedOption(sel) {
var opt;
for ( var i = 0, len = sel.options.length; i < len; i++ ) {
opt = sel.options[i];
if ( opt.selected === true ) {
break;
}
}
return opt;
}
// assign onclick handlers to the buttons
document.getElementById('showVal').onclick = function () {
el.value = sel.value;
}
document.getElementById('showTxt').onclick = function () {
// access text property of selected option
el.value = sel.options[sel.selectedIndex].text;
}
document.getElementById('doLoop').onclick = function () {
var opt = getSelectedOption(sel);
el.value = opt.value;
}
}());
// immediate function to preserve global namespace
Also in JavaScript:
- onclick open link js
- get json by id
- react datetime mannual editing
- return uncommon from two arrays js
- array includes
- react native get numeric random string length of 5 characters
- jquery vs react
- js string replace
- how to check if a number is negative in p5.js
- Send redirect URL in the text body of mail using nodemailer
- stop window.setinterval javascript
- how to create a server in node js
- jquery set attribute stack overflow
- transform array to string js
- falsy values javascript
- how to access the second class of an element javascript
- import img react
- js slice string at word
- express js params
- format money javascript commas
- ajax cdn
- div background image chnage uisng js
- template literal syntax' is only available in ES6 (use 'esversion: 6'). (W119)jshint(W119)
- laravel json response with error code