Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to add values in list box from client (javascript and jquery).

What I have tried:

I tried both js and jq , but both not working,
my code in JQ
JavaScript
var value = 'aravind';
var listBox = $("lstProfLstS1");
var option = $("<option />").val(value).html(value);
listBox.append(option);
return false;

my code in JS
JavaScript
var Val1='aravind';
var opt1 = document.createElement("option");
opt1.innerHTML = Val1.replace(/ /g, "\xA0");
opt1.value = Val1;
document.getElementById("lstProfLstS1").options.add(opt1);


Regards,
Aravind
Posted
Updated 26-Feb-19 23:00pm
Comments
CHill60 27-Feb-19 3:29am    
"but both not working," - what is actually happening?
Aravindba 27-Feb-19 4:02am    
values not added in listbox,means listbox is empty

Now we know this is an ASP control, see Karthik's comment "you won't be able to get the client side added values in the server side (Post back)

However, if you want to access the items in the code behind then use a hidden field to capture the items that are added client side. You can then query that hidden field server side.

Edit - I've found this worked example[^]
 
Share this answer
 
v2
Comments
Aravindba 27-Feb-19 5:32am    
yes correct we get values in hidden fields.
Checkout these
Using JS

<html xmlns="http://www.w3.org/1999/xhtml"> 
<body> 
    <select id="lstProfLstS1" ></select> 
    <script>
        var Val1 = 'aravind';
        var opt1 = document.createElement("option");
        opt1.text = Val1;
        opt1.value = Val1;
        document.getElementById("lstProfLstS1").options.add(opt1); 
    </script>
  
</body>
</html>


Using JQuery
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js" ></script>
    <script>
        $(function () {
            var value = 'aravind';
            var listBox = $("#lstProfLstS1");
            var option = $("<option />").val(value).text(value);
            listBox.append(option); 
        });
    </script>
</head>
<body>
    <select id="lstProfLstS1"></select>
    

</body>
</html>
 
Share this answer
 
Comments
Aravindba 27-Feb-19 4:03am    
hi this is my control
<asp:ListBox ID="lstProfLstS1" Width="100%" Height="200px" runat="server">
Karthik_Mahalingam 27-Feb-19 4:12am    
if it is a asp control, you wont be able to get the client side added values in the server side ( Post back)
Aravindba 27-Feb-19 5:32am    
hi we added value in hidden field and used in server side, below mention CHill60 is correct, we did like that.my problem in only not display in listbox when add, but we can store values,bcz i store in hidden field.
Karthik_Mahalingam 27-Feb-19 6:17am    
are you using masterpage? if so use like this
document.getElementById('# <%= lstProfLstS1.ClientID %>');
refer this https://www.dotnetodyssey.com/2014/12/24/get-id-asp-net-server-control-using-javascript-jquery/
Aravindba 27-Feb-19 22:35pm    
no, i am not using master page,if problem in getting control id it show undefined error, but my code it append values (document.getElementById("lstProfLstS1").options.add(opt1)) and also next line i put return false also.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900