parameter not passed by jQuery

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • svendok
    New Member
    • Jun 2010
    • 25

    parameter not passed by jQuery

    Can anyone tell me why the selectedLength function is failing in the code below?

    It can be played with here: http://jsbin.com/oluyu6/3/edit

    Thanks.

    Svend


    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <meta charset=utf-8 />
    <title>JS Bin</title>
    <style type="text/css">
    <!--
    
    option.emphasised {
        background-color: white;
        color: blue;
        font-style: oblique;
    }
    
    -->
    </style>
    
      <script>
     function selectedLength(box)
    {
             selected = [];
             for(var i=0; i< box.options.length;i++)
             {
                 if(box.options[i].selected) selected.push( box.options[i].value);
             }
             return selected.length;
    }
    
    function toggleArrowBySelectedVal(option_array, list, arrow)
    {
    
      if ( (selectedLength(list)==1)   && 
         (option_array.find(Number(list.value))))  arrow.disabled=false;
      else                      arrow.disabled=true;
      
    }
        
        
      window.onload = (function(){
       $("#first").change(function() {
         toggleArrowBySelectedVal([8,15],$("#first"),$("#expandPlus1PDC"));});
      })
          </script>
    <!--[if IE]>
      <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <style>
      article, aside, figure, footer, header, hgroup, 
      menu, nav, section { display: block; }
    </style>
    </head>
    <body>
      <p id="hello">Hello World</p>
      
      <SELECT NAME="first" SIZE=13 multiple id="first"  selectedIndex=-1>
    <option value="1" class="">A</option>
    <option value="2" class="">B</option>
    <option value="8" class="emphasized">C</option>
    <option value="15" class="emphasized">D</option>
    <option value="10" class="">E</option>
    
    </select>
      <input name="expandPlus1PDC" type="button" disabled id="expandPlus1PDC" title="Expand list" value=">>">
      
    </body>
    </html>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    because $("#first") is a jQuery object and not a <select> object.

    Comment

    • svendok
      New Member
      • Jun 2010
      • 25

      #3
      Thanks for the response. I'm (probably obviously) a newbie to jQuery. What syntax should I be using instead?

      I find it weird, because a bunch of other functions with form objects passed this way seem to work fine. Is there a different syntax because the object involved is bound to the function? Were it normal JS, I'd just use "this", but didn't work.

      Thanks.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        $("#first option:selected ").length, why working around something jQuery has a syntax for.

        Comment

        • svendok
          New Member
          • Jun 2010
          • 25

          #5
          Well, the thing is that I'm trying to make these functions refactor-able. If I use $("#first option:selected ").length inside the selectedLength( ) function, it obviously becomes a one-off function.

          Comment

          • svendok
            New Member
            • Jun 2010
            • 25

            #6
            Oh, I see what you mean. I solved it with this:
            var numselected=$(" #"+list_id+" option:selected ").length;

            which removed that 2nd function entirely.

            Thanks.

            Comment

            Working...