I have a button which opens a popup window and lets me to choose something from a list and then returns to parent page with the ID and NAME variables. In the parent page, I store ID in an hidden field and NAME in a textbox.
This is ok when i choose one thing from list. But now I want to choose multiple things and store ID’s and NAME’s in a listbox value and text property.
How can I do that?
These are the javascript codes I am using for textbox and hidden field.
<script type="text/javascript">
function get_selected_machine(ID, NAME) {
document.getElementById("txtName").value = NAME;
document.getElementById("hdnID").value = ID;
}
</script>
Below is a very simple example that illustrates how to get the values from a listbox using pure JavaScript. How you decide to store the values on the client once you have them is up to you.
HTML:
JavaScript
Here’s a working jsFiddle.
Note: The jsFiddle uses jQuery, but only as a means of attaching to the click event of the button. I did not use jQuery for anything else because I’m assuming that you want a pure JS solutions (you didn’t tag jQuery in your post).