Kumar AU

Kumar AU

  • 1.4k
  • 299
  • 58.4k

Add and Remove text box value to ListBox Items in JavaScript

Aug 6 2014 8:52 AM
Can any one please tell me in ASP.NET How to ADD or REMOVE text box value to List Box item using Add and Remove buttons this should be done using Java script only without server post back.
 
This is my code, but its not working.
 
ASPX :-
<input type="button" value="Add Item" onclick="javascript:addValue()" />
<input type="button" value="Delete Item" onclick="javascript:deleteValue()" />
 <asp:TextBox ID="txtValue" runat="server"></asp:TextBox>
<asp:ListBox ID="LstChanelType" runat="server"></asp:ListBox> 
 
 
Javascript :-  
<script type="text/javascript">
var i = 0;
function addValue() {
var v = document.form1.txtChanelType.value;
// get the TextBox Value and assign it into the variable
AddOpt = new Option(v, v);
document.form1.LstChanelType.options[i++] = AddOpt;
return true;
}
function deleteValue() {
var s = 1;
var Index;
if (document.form1.LstChanelType.selectedIndex == -1) {
alert("Please select any item from the ListBox");
return true;
}
while (s > 0) {
Index = document.form1.LstChanelType.selectedIndex;
if (Index >= 0) {
document.form1.LstChanelType.options[Index] = null;
--i;
}
else
s = 0;
}
return true;
}
</script>
 
 
Always here i am getting an error message  :- 
 Compiler Error Message: CS1026: ) expected
 Line 192: <asp:Button ID="add" Text="Add" CssClass="form_submit" runat="server" onclick="javascript:addValue()" />
 
Please please help me...
 
 
 

Answers (1)