Accessing dropdown list with multiple selection option

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
lokesh_kumar_s
Forum Commoner
Posts: 48
Joined: Mon Apr 13, 2009 5:39 am
Contact:

Accessing dropdown list with multiple selection option

Post by lokesh_kumar_s »

hi i am having an drop down list which is an multiple selection dropdown list. and user should select atleast one option in that and maximum of 5. i want to do this using javascript please help me.

Code: Select all

 
<select multiple="multiple" name="DrpEmployee[]" style="width: 173px">
<option>1<option>
<option>2<option>
<option>3<option>
<option>4<option>
<option>5<option>
<option>6<option>
<option>7<option>
<select>
 
how can i check whether user selected atleast one option or less than 5 please help
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Accessing dropdown list with multiple selection option

Post by VladSun »

[js]        function onSelChange(cb)        {            var count = 0            for (var i=0, len=cb.options.length; i<len; i++)            {                if (cb.options.selected)                    count++;            }                    } [/js]
There are 10 types of people in this world, those who understand binary and those who don't
lokesh_kumar_s
Forum Commoner
Posts: 48
Joined: Mon Apr 13, 2009 5:39 am
Contact:

Re: Accessing dropdown list with multiple selection option

Post by lokesh_kumar_s »

VladSun wrote:[js]        function onSelChange(cb)        {            var count = 0            for (var i=0, len=cb.options.length; i<len; i++)            {                if (cb.options.selected)                    count++;            }                    } [/js]

gives me this message
"options.length is null or not an object"
why am i getting this error
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Accessing dropdown list with multiple selection option

Post by VladSun »

cb must be a SELECT DOM Object
There are 10 types of people in this world, those who understand binary and those who don't
lokesh_kumar_s
Forum Commoner
Posts: 48
Joined: Mon Apr 13, 2009 5:39 am
Contact:

Re: Accessing dropdown list with multiple selection option

Post by lokesh_kumar_s »

VladSun wrote:cb must be a SELECT DOM Object
please correct me i have written my code

Code: Select all

 
<form name="myform">
# <select multiple="multiple" name="DrpEmployee[]" style="width: 173px">
# <option>1<option>
# <option>2<option>
# <option>3<option>
# <option>4<option>
# <option>5<option>
# <option>6<option>
# <option>7<option>
# <select>
 
<input type="submit" name="submit" value="submit" onclick="return Validate(this.form)">
<script type="text/javascript">
 
function Validate(frm)
{
    var count = 0;
    for (var i=0, len=theForm.DrpEmployee.options.length; i<len; i++)
    {
               if (myform.DrpEmployee.options[i].selected)
                  count++;
    }
    alert(theForm.DrpEmployee.options.length);
    alert(count);
 
}
 
</script>
 
 
giving me error "theForm.DrpEmployee.options is null or not an object"

please help
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Accessing dropdown list with multiple selection option

Post by VladSun »

I don't know about your JS code but your HTML is full of errors
Tags should have closing tags, like:
<tag> ........ </tag>

First, fix your HTML - SELECT and OPTION tags, then try it.
Also, what is the purpose of those # symbols there? They also break your HTML
There are 10 types of people in this world, those who understand binary and those who don't
lokesh_kumar_s
Forum Commoner
Posts: 48
Joined: Mon Apr 13, 2009 5:39 am
Contact:

Re: Accessing dropdown list with multiple selection option

Post by lokesh_kumar_s »

VladSun wrote:I don't know about your JS code but your HTML is full of errors
Tags should have closing tags, like:
<tag> ........ </tag>

First, fix your HTML - SELECT and OPTION tags, then try it.
Also, what is the purpose of those # symbols there? They also break your HTML

Code: Select all

 
<form name="myform">
<select multiple="multiple" name="DrpEmployee[]" style="width: 173px">
<option>1<option>
<option>2<option>
<option>3<option>
<option>4<option>
<option>5<option>
<option>6<option>
<option>7<option>
</select>
  
 <input type="submit" name="submit" value="submit" onclick="return Validate(this.form)">
 </form>
 <script type="text/javascript">
  
 function Validate(frm)
 {
     var count = 0;
     for (var i=0, len=frm.DrpEmployee.options.length; i<len; i++)
     {
               if (frm.DrpEmployee.options[i].selected)
                  count++;
    }
    alert(frm.DrpEmployee.options.length);
    alert(count);
 
}
  
</script>
 
 
 
giving me error "theForm.DrpEmployee.options is null or not an object"

please provide me the solution i have corrected the code
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Accessing dropdown list with multiple selection option

Post by VladSun »

lokesh_kumar_s wrote:please provide me the solution i have corrected the code
No, you haven't - you still haven't closed the OPTION tags.

Code: Select all

name="DrpEmployee[]"

Code: Select all

frm.DrpEmployee
You see - you are trying to access not existing property frm.DrpEmployee, because the name of the existing property is DrpEmployee[].

Accessing properties with names like this is done by using the object like an associative array:
E.g.
[js]object['property'] = value;[/js]
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply