I have a select input where I am adding another option via jQuery
,
The steps I am following here is creating an option
element by documents createElement
method, then adding some innerHTML
and at last, setting the value
.
The problem is at the second step, when I just created an element and added the innerHTML
only ( didn't add the value yet ), chrome consoles the element with the value
which I have added in the next step.
How does chrome do this ? and why ?
Check the code snippet below:
Note: The snippet here shows it corretly, but chrome browser doesn't when I am running the code from an individual .html
file.
Firefox shows the correct output though.
$(document).ready(function() {
$('#addSteve').on('click', function() {
var newVal = document.createElement('option');
newVal.text = 'Steve';
console.log(newVal); // didn't assign a value 'hello', still consoles it;
newVal.value = 'Hello';
console.log(newVal); // supposed to console the value here only;
$('#wrapper').find('select').append(newVal);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
<select>
<option value="John" selected>John</option>
<option value="Nick">Nick</option>
<option value="Gomez">Gomez</option>
</select>
</div>
<button type="button" id="addSteve">Add Steve</button>