...

View Full Version : Get the value from a select form



maxelcat
09-30-2008, 02:33 PM
Dear All

I want to get the value from a select option in a form. It works in FF but not in IE7 which just returns a blank alert window.

I suspect I am doing something pretty stupid!

Any ideas please!

Here's the html



<script src="pafForm.js" type="text/javascript"></script>
<title>Untitled Document</title>
</head>
<body>
<form action="contact.html" name="pafEnquiries" method="post">
<select id="projectType">
<option>Please select</option>
<option>Commercial</option>
<option>Residentail</option>
<option>Industrial</option>
</select>
</form>


and here's the js



window.onload = initForm;

function initForm(){
document.getElementById("projectType").selectedIndex=0;
document.getElementById("projectType").onchange=populateApplication;
}
function populateApplication(){

var edNum = document.getElementById("projectType").selectedIndex;
var edValue = document.getElementById("projectType").options[edNum].value;
alert (edValue);
}

shyam
09-30-2008, 05:17 PM
<select id="projectType">
<option>Please select</option>
<option>Commercial</option>
<option>Residentail</option>
<option>Industrial</option>
</select>
</form>


did you try setting the options value attribute


<select id="projectType">
<option value="Please select">Please select</option>
<option value="Commercial">Commercial</option>
<option value="Residentail">Residentail</option>
<option value="Industrial">Industrial</option>
</select>
</form>

rnd me
09-30-2008, 06:15 PM
no need to recode, just fix your bug:

var edValue = document.getElementById("projectType").options[edNum].value;

should be:
var edValue = document.getElementById("projectType").options[edNum].text;

maxelcat
10-02-2008, 10:48 AM
Thanks to both of you.

I sorted it by adding the value attribute, but I could see how the .text fix would work too!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum