Page 1 of 1

Accessing dropdown list with multiple selection option

Posted: Fri Jul 03, 2009 9:01 am
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

Re: Accessing dropdown list with multiple selection option

Posted: Fri Jul 03, 2009 9:54 am
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]

Re: Accessing dropdown list with multiple selection option

Posted: Fri Jul 03, 2009 11:27 pm
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

Re: Accessing dropdown list with multiple selection option

Posted: Sat Jul 04, 2009 1:30 am
by VladSun
cb must be a SELECT DOM Object

Re: Accessing dropdown list with multiple selection option

Posted: Sat Jul 04, 2009 6:41 am
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

Re: Accessing dropdown list with multiple selection option

Posted: Sat Jul 04, 2009 8:16 am
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

Re: Accessing dropdown list with multiple selection option

Posted: Sun Jul 05, 2009 8:53 pm
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

Re: Accessing dropdown list with multiple selection option

Posted: Mon Jul 06, 2009 2:08 am
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]