473,394 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

parameter not passed by jQuery

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.

5 2017
Dormilich
8,658 Expert Mod 8TB
because $("#first") is a jQuery object and not a <select> object.
Apr 13 '11 #2
svendok
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
8,658 Expert Mod 8TB
$("#first option:selected").length, why working around something jQuery has a syntax for.
Apr 13 '11 #4
svendok
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
svendok
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

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

Similar topics

1
by: Adam Dyga | last post by:
Hi, How to create function with optional parameter passed by reference? I've tried sth like this: function (&$param=NULL) // or: function (&$param=new array()) { //... }
7
by: Richard Cavell | last post by:
Hi, The point of using const on a parameter to a function should be to let your compiler know that the parameter shouldn't be modified during your program. This allows you to keep your code...
1
by: bmeyynospamnospam | last post by:
I am new to sql and very familiar with access. I am using a very large database(130M records) in ms sql2000 and think I need to front end it with access for reports and forms, etc.. I have some...
0
by: clarissa | last post by:
How do I pass parameters to extract consecutive search result pages from the results given by Google API?
2
by: ansonee | last post by:
I have the follwoing stored procedure: ALTER procedure .( @ExecutionGUID int = null ) as begin set nocount on declare @s varchar(500) declare @i int
3
Ajm113
by: Ajm113 | last post by:
How do I have it so if a word in the url was entered then php will run a part of a script? Example: Since I want to save server space I want to be able to update my database in one file. Which...
1
by: fbnewtz | last post by:
I am trying to modify someone elses code that skipped out in the middle of a project and am fairly new to ASP.net. This seems like a simple insert stored proceedure on a pretty basic form. For some...
0
by: crislu | last post by:
Hi, I need to know if someone knows what this error means. It is of the Development Kit of RCM4000 of Rabbit Semiconductor. I'd tried to use the sample programs of this board, but I'd tried to...
14
by: Mathijs | last post by:
Hi group, I have a class (TheClass) with static methods from an external DDL. private void Test(ref byte param) { byte a = 0, b = 0; param = 1; TheClass.CallToDLLFunction(ref a, ref b);...
5
by: rpelletier | last post by:
Good morning....As you will see when you view the script I am fairly new to Java. All I am trying to accomplish with this script is to navigate to the static URL listed in the script, however, one of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.