Click here to Skip to main content
15,917,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a dropdownlist like this

<asp:dropdownlist id="DropDownList1" runat="server" onchange="select();" autopostback="True" style="z-index: 128;<br mode=" hold=" /> left: 659px; position: absolute; top: 244px" xmlns:asp="#unknown">
<asp:listitem>--select--
<asp:listitem>1
<asp:listitem>2
<asp:listitem>3
<asp:listitem>4
<asp:listitem>5
<asp:listitem>6
<asp:listitem>

i want that once the user selected the item he/she
will not be able to select that same item second time using java script
i tried ur javascript ie



function select()
{

var item = document.getElementById("DropDownList1").value;
if (item != "Select") {
var selected = document.getElementById("seletedItems").innerHTML;
if (selected.search(item) == -1) {
document.getElementById("seletedItems").innerHTML += item + ",";
}
else {
alert("item already selected");
}
}


and also an error is coming on to the line onchange="select();" stating that invalid attribute of dropdownlist...
ur code is not working plz help with some diff soln
Posted
Updated 2-Aug-10 6:02am
v2
Comments
raju melveetilpurayil 31-Jul-10 10:59am    
store selected item in any variables, check that variable in next selection
raju melveetilpurayil 3-Aug-10 15:52pm    
my code did work. did you add div control?
raju melveetilpurayil 3-Aug-10 15:54pm    
i gave working code. Just do copy paste, it will work fine.
raju melveetilpurayil 4-Aug-10 4:57am    
and why you update this question with my answer?

This will help you.
XML
<div id="seletedItems" style="display:none"></div>
   <asp:DropDownList ID="DropDownList1" runat="server" onchange="Select();">
   <asp:ListItem>Select</asp:ListItem>
       <asp:ListItem>1</asp:ListItem>
       <asp:ListItem>2</asp:ListItem>
       <asp:ListItem>3</asp:ListItem>
       <asp:ListItem>4</asp:ListItem>
       <asp:ListItem>5</asp:ListItem>
   </asp:DropDownList>
   <script type="text/javascript">
       function Select() {
           var item = document.getElementById("DropDownList1").value;
           if (item != "Select") {
               var selected = document.getElementById("seletedItems").innerHTML;
               if (selected.search(item) == -1) {
                   document.getElementById("seletedItems").innerHTML += item + ",";
               }
               else {
                   alert("item already selected");
               }
           }
       }
   </script>
 
Share this answer
 
Comments
software_Engi08 7-Aug-10 14:30pm    
yes i have copy paste that code also i have added div but still it is not working.wen i m selected an item
also page is not going for a postback .also i have enabled dropdownlist autopostback to true.
i want code using java only plz help
raju melveetilpurayil 7-Aug-10 14:44pm    
if you add autopostback to true then why to trying to do validation in client side? do validation in server side.
1. Maintain the selected item in a hidden variable. Maintain it using JavaScript. Next time on selection of it, show appropriate message or take appropriate step
2. On selecting the values, remove it from the dropdown using JavaScript such that they are no more selectable.

Try now!
 
Share this answer
 

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