Is it possible to get a form elements value using javascript if the action goes to a different page?
Here are snippets of code to illustrate what I’m trying to do:
index.html:
<form name="testMe" action="show_music.jsp" method="get">
<p>I am interested in these types of music:</p>
<select id="music" name="music" multiple>
<option value="classical">Classical</option>
<option value="christian">Christian</option>
<option value="alternative">Alternative</option>
<option value="rock">Rock</option>
<option value="latin">Latin</option>
<option value="pop">Pop</option>
<option value="disco">Disco</option>
</select>
show_music.jsp
<p>Here are the music styles you like: </p>
<script type="text/javascript">
var e = document.getElementById("music");
var value = e.options[e.selectedIndex].text;
alert("Var is: " + value);
</script>
I’ve been trying different combinations of trying to get the values of that select list, but no luck. Is it even possible? Thanks in advance.
In this cases it’s better to use server-side to gather the value, so if possible, use POST instead of GET, using JSP (and you can pass the value to Javascript easily):
Or if you can’t/don’t want to use POST, use query string parsing like some suggested