By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,654 Members | 1,554 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,654 IT Pros & Developers. It's quick & easy.

parameter not passed by jQuery

P: 25
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


Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  5. <meta charset=utf-8 />
  6. <title>JS Bin</title>
  7. <style type="text/css">
  8. <!--
  9.  
  10. option.emphasised {
  11.     background-color: white;
  12.     color: blue;
  13.     font-style: oblique;
  14. }
  15.  
  16. -->
  17. </style>
  18.  
  19.   <script>
  20.  function selectedLength(box)
  21. {
  22.          selected = [];
  23.          for(var i=0; i< box.options.length;i++)
  24.          {
  25.              if(box.options[i].selected) selected.push( box.options[i].value);
  26.          }
  27.          return selected.length;
  28. }
  29.  
  30. function toggleArrowBySelectedVal(option_array, list, arrow)
  31. {
  32.  
  33.   if ( (selectedLength(list)==1)   && 
  34.      (option_array.find(Number(list.value))))  arrow.disabled=false;
  35.   else                      arrow.disabled=true;
  36.  
  37. }
  38.  
  39.  
  40.   window.onload = (function(){
  41.    $("#first").change(function() {
  42.      toggleArrowBySelectedVal([8,15],$("#first"),$("#expandPlus1PDC"));});
  43.   })
  44.       </script>
  45. <!--[if IE]>
  46.   <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  47. <![endif]-->
  48. <style>
  49.   article, aside, figure, footer, header, hgroup, 
  50.   menu, nav, section { display: block; }
  51. </style>
  52. </head>
  53. <body>
  54.   <p id="hello">Hello World</p>
  55.  
  56.   <SELECT NAME="first" SIZE=13 multiple id="first"  selectedIndex=-1>
  57. <option value="1" class="">A</option>
  58. <option value="2" class="">B</option>
  59. <option value="8" class="emphasized">C</option>
  60. <option value="15" class="emphasized">D</option>
  61. <option value="10" class="">E</option>
  62.  
  63. </select>
  64.   <input name="expandPlus1PDC" type="button" disabled id="expandPlus1PDC" title="Expand list" value=">>">
  65.  
  66. </body>
  67. </html>
  68.  
Apr 13 '11 #1

✓ answered by Dormilich

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

Share this Question
Share on Google+
5 Replies


Dormilich
Expert Mod 5K+
P: 8,639
because $("#first") is a jQuery object and not a <select> object.
Apr 13 '11 #2

P: 25
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.
Apr 13 '11 #3

Dormilich
Expert Mod 5K+
P: 8,639
$("#first option:selected").length, why working around something jQuery has a syntax for.
Apr 13 '11 #4

P: 25
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.
Apr 14 '11 #5

P: 25
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.
Apr 14 '11 #6

Post your reply

Sign in to post your reply or Sign up for a free account.