Get dropdown list label value
by RedCoupe on Jun.09, 2011, under Web design / development
I was working on a couple of web pages this morning to allow completion of a course booking form, user then submits to a thankyou page, confirmation emails are sent etc.
The problem I faced was that only the course ID was being passed to the thankyou page so in the emails that are generated, it displays Course Details: 7 , instead of Course Details: 12/12/2011 PHP Basics.
So my initial thought was to create a hidden field just before the end form tag together with some javascript that gets the relevant label value:
document.getElementById('coursedetails').value = document.getElementById('training_id')[document.getElementById('training_id').selectedIndex].innerHTML;
Then in the thankyou page I just got Request.Form(“coursedetails”), this is in ASP VBScript just in case you were wondering. Works like a charm.
Since doing this is I should have done it a much easier way and not have to rely on JavaScript being enabled. I already had the value because I was already using it to display the dropdown list label. So the hidden field could have just contained the default value of the currently selected item.