---
title: "Binding Data in Listbox And Get Selected Value From Listbox Using Java Script"  
description: "In this blog I will explain how to add item in listbox by using Java script and how to get selected item from the HTML listbox.  First create a simple"  
author: "Chris Anderson"  
published: 2011-09-07  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/232/binding-data-in-listbox-and-get-selected-value-from-listbox-using-java-script  
category: "javascript"  
tags: ["javascript"]  
reading_time: 2 minutes  

---

# Binding Data in Listbox And Get Selected Value From Listbox Using Java Script

In this blog I will [explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) how to add item in [listbox](https://www.mindstick.com/articles/626/listbox-events-in-c-dot-net) by using Java script and how to get selected item from the HTML listbox.\

##### First create a simple listbox and a button:

```
        <table>        <tr>            <td valign="top">Select Product: </td>            <td>                <select size="5" id="listProduct">                </select>            </td>        </tr>        <tr>            <td colspan="2">                <input type="button" value="Enter"/>            </td>        </tr>
    </table>
```

Create a [JavaScript function](https://www.mindstick.com/forum/2090/send-data-from-asp-dot-net-code-behind-to-a-javascript-function) that [add items](https://www.mindstick.com/forum/160381/how-to-add-items-to-a-list-collection-in-c-sharp) in java script and call that function on the form load:

```
      function addProduct() {
            var arrProduct = ["KeyBoard", "Monitor", "USB Drive", "Speaker", "Mouse"];
            for (var i = 0; i < arrProduct.length; i++) {
                var opt = document.createElement("option");
                document.getElementById("listProduct").options.add(opt);
                opt.text = arrProduct[i];
                opt.value = arrProduct[i];
            }
        }   <body onload="addProduct()"> 
```

Then create a JavaScript function that get a selected item from a listbox and display it in [alert box](https://www.mindstick.com/articles/12940/how-to-create-message-alert-box-using-jquery), also call that function on the click of [button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net):

```
   function getSelectedProduct() {
            var list = document.getElementById('listProduct');
            for (var i = 0; i < list.options.length; ++i) {
                if(list.options[i].selected)
                    alert(list.options[i].value);
            }
        }     
```

<input type="button" value="Enter" [onclick](https://www.mindstick.com/forum/12718/onclick-for-hyperlink)="getSelectedProduct()" />

Thank you for [reading](https://www.mindstick.com/articles/126273/books-commonly-found-on-college-reading-lists) this blog and I think this will help you a lot.

---

Original Source: https://www.mindstick.com/blog/232/binding-data-in-listbox-and-get-selected-value-from-listbox-using-java-script

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
