user1690748 user1690748 - 2 months ago 9
HTML Question

<option> value to <input> text value

I am trying to set a input box's value and text from a select element.

var p = document.getElementById("pretestNameSelect");
var test = p.options[p.selectedIndex].value;
if (choice.toString() == "type0")
{
alert(test);
$(document.getElementById("type0")).show();
hideOtherQuestionDivs(choice.toString());
$(document.getElementById("pretestNameSelectHiddenTextQ0")).value = "test";
$(document.getElementById("pretestNameSelectHiddenTextQ0")).text = "test";
alert(document.getElementById("pretestNameSelectHiddenTextQ0").value);
}


The alert(test) works and shows the correct value but the value is not set and the alert(doc...) shows an empty alert.

What could be the problem?

Answer

You are mixing Jquery Selectors with DOM selectors.

You can use

$('#pretestNameSelectHiddenTextQ0').val(test);

or

document.getElementById('pretestNameSelectHiddenTextQ0').value = test;
Comments