I have a code snippet where I want to get the value of option
selected with JavaScript, and give alert
with that value. However, every time it gives the value as undefined
. I made a line to put in a input
too, just to test, and it did not work.
Here is the code:
$("select[name='estado']").change(function() {
var estado = document.getElementsByTagName("estado").value;
document.getElementById("teste").setAttribute("value", estado);
alert('estado ' + estado);
});
<select id="txt-model" class="field-50" name="estado">
<option value="">Selecione um Estado</option>
<option value="AC">Acre</option>
<option value="AL">Alagoas</option>
<option value="AP">Amapá</option>
//... O resto dos estados
</select>
<input type="text" id="teste" name="teste">