I want my select
element to always show the same Text, no matter which option is selected. How would i do that?
<select>
<option defaultValue disabled>
Add library
</option>
<option value="code.jquery.com/jquery-3.1.0.js">
JQuery 3.1.0
</option>
<option value="https://cdn.jsdelivr.net/lodash/4/lodash.min.js">
lodash 4.x
</option>
<option value="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.14/require.js">
RequireJS
</option>
</select>
Just take the initial value and store it in a variable. And then, whenever the change
event on select is called re-assign the previous value to the select.
var select = document.querySelector('select');
var value = select.value;
select.addEventListener('change', function(event) {
select.value = value;
console.log('changed');
});
<select>
<option defaultValue disabled>
Add library
</option>
<option value="code.jquery.com/jquery-3.1.0.js">
JQuery 3.1.0
</option>
<option value="https://cdn.jsdelivr.net/lodash/4/lodash.min.js">
lodash 4.x
</option>
<option value="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.14/require.js">
RequireJS
</option>
</select>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments