problem description
newcomer, I encountered a variable reference problem when learning to delete option from the select list, as follows:
1. Use option [index] = null, you can achieve the purpose of deletion;
2. Assign option [index] to a variable, and then set the variable to invalid null, for example:
var a = option[index];
a = null;
after thinking, I have realized that an actually refers to the address of option [index]. Setting a = null, only changes the reference address of a, and option [index] has not actually changed. However, I can"t figure out the following question. The code is posted below.
Code
<form action="" name="theForm">
<select name="theDay">
<option value="1">1</option>
</select>
</form>
<script>
var option = document.theForm.theDay.options[0];
document.theForm.theDay.options[0] = null;
console.log(document.theForm.theDay.options[0]); // undefined
console.log(option); // <option value="1">1</option>
</script>
and I thought the output would be
console.log(document.theForm.theDay.options[0]); // null
console.log(option); // null
I haven"t figured it out for a long time. I hope God will give me some advice
.