I am trying to create a little HTML form where my users need to select some options from checkboxes and dropdown lists. The intended outcome is a text that is generated dynamically as the users change their choices. The idea does work but I keep getting "[object HTMLSelectElement]" instead of the value of the selected choice from the dropdown box. If one of you veteran JavaScript programmers could give me your opinion I’d greatly appreciate it! I spent quite some time double checking everything and I’m pretty sure my code is correct syntactically but I could be completely wrong without realizing it. I’m new at JavaScript so I’m still in a learning phase.
HTML Code:
<form name="input" action="" method="get">
<input type="checkbox" name="stCode" id="ai1" onChange="generateSTcode()">
Analog Input B1
<select name="sensorType" id="sensorType1" onChange="generateSTcode()">
<option value="2">0-1 Vdc</option>
<option value="3">0-10 Vdc</option>
<option value="4">4-20mA</option>
<option value="5">Off/On</option>
<option value="6">0-5 Vdc</option>
</select>
<select name="sensor" id="sensor1" onChange="generateSTcode()">
<option value="oatp_ai">Outside air temperature</option>
<option value="satp_ai">Supply air temperature</option>
<option value="ratp_ai">Return air temperature</option>
<option value="mxtp_ai">Mixed air temperature</option>
</select><br><br>
<input type="checkbox" name="stCode" id="ai2" onChange="generateSTcode()">
Analog Input B2
<select name="sensorType" id="sensorType2" onChange="generateSTcode()">
<option value="2">0-1 Vdc</option>
<option value="3">0-10 Vdc</option>
<option value="4">4-20mA</option>
<option value="5">Off/On</option>
<option value="6">0-5 Vdc</option>
</select>
<select name="sensor" id="sensor2" onChange="generateSTcode()">
<option value="oatp_ai">Outside air temperature</option>
<option value="satp_ai">Supply air temperature</option>
<option value="ratp_ai">Return air temperature</option>
<option value="mxtp_ai">Mixed air temperature</option>
</select><br><br>
<input type="checkbox" name="stCode" id="ai3" onChange="generateSTcode()">
Analog Input B3
<select name="sensorType" id="sensorType3" onChange="generateSTcode()">
<option value="2">0-1 Vdc</option>
<option value="3">0-10 Vdc</option>
<option value="4">4-20mA</option>
<option value="5">Off/On</option>
<option value="6">0-5 Vdc</option>
</select>
<select name="sensor" id="sensor3" onChange="generateSTcode()">
<option value="oatp_ai">Outside air temperature</option>
<option value="satp_ai">Supply air temperature</option>
<option value="ratp_ai">Return air temperature</option>
<option value="mxtp_ai">Mixed air temperature</option>
</select><br><br>
</form>
<div id="generatedCode" class="generatedCode"></div>
JavaScript Code:
function generateSTcode(){
var stCodegenerated = "";
var analogInputCh = new Array(3);
analogInputCh[0] = document.getElementById('ai1');
analogInputCh[1] = document.getElementById('ai2');
analogInputCh[2] = document.getElementById('ai3');
var sensorType = new Array(3);
sensorType[0] = document.getElementById('sensorType1');
sensorType[1] = document.getElementById('sensorType2');
sensorType[2] = document.getElementById('sensorType3');
var sensor = new Array(3);
sensor[0] = document.getElementById('sensor1');
sensor[1] = document.getElementById('sensor2');
sensor[2] = document.getElementById('sensor3');
for(i = 0; i < analogInputCh.length; i++){
if(analogInputCh[i].checked){
var iTemp = i + 1;
stCodegenerated += "Ain_Conf(" + iTemp + ", " + sensorType[i] + ", AI_0" + iTemp + "); "
+ sensor[i] + " := AI_0" + iTemp + ";" + "<br><br>";
}
}
document.getElementById("generatedCode").innerHTML = stCodegenerated;
}
2 Answers 2
It's because you need to use .value property of HTMLElement.
Example below:
var myElement = document.getElementById('myElement'),
myElementValue = myElement.value;
And so on.
Notice that value works only with inputs, if you want to get the content of a tag you should use innerHTML.
EDIT:
I edited your code, now it works well(there was a var before declaring i in the for loop that could cause problems) and this is your updated JSFiddle
Comments
sensorType and sensor are arrays of select elements. The currently selected value of a select element is stored in its value. Change the code inside the for loop to the following:
stCodegenerated += "Ain_Conf(" + iTemp + ", " + sensorType[i].value + ", AI_0" + iTemp + "); "
+ sensor[i].value + " := AI_0" + iTemp + ";" + "<br><br>";
Then each time a user changes a state of an element, you will get something like this:
Ain_Conf(1, 2, AI_01); oatp_ai := AI_01;
Hope this will help
.valueto get the value