473,406 Members | 2,371 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,406 software developers and data experts.

Select Box & JavaScript Functions Question

Hello All,

I have a multi-select list box, In this box is a list of names. I want to call a javascript onClick that will keep a running total of how many people were clicked. As soon as a user tries to click the 41st item (only 40 allowed) I want to alert the user, they cannot select more then 40 participants in this box.

Here is what i have so far:

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function KeepTotal(){
  3.    var i;
  4.    i = i + 1;
  5.    if (i > 40)
  6.    {
  7.       alert('You can only select 40 names');
  8.    }
  9. }
  10. </script>
  11. <select name="participants" onClick="javascript:KeepTotal();" multiple>
  12.    <option value="1">Name 1</option>
  13.    <option value="2">Name 2</option>
  14.    ... (about 150 names in total populated from a db)
  15. </select>
  16.  
can someone please explain to me where im going wrong and how to get this working. Im not looking to have it done for me without an explanation. I would love to understand this as im sure I will need to write more functions like this in the future.

thanks,
John
May 11 '07 #1
5 1577
iam_clint
1,208 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. var i = 0;
  3. function KeepTotal(){
  4.    i += 1;
  5.    if (i > 40)
  6.    {
  7.       alert('You can only select 40 names');
  8.    }
  9. }
  10. </script>
  11. <select name="participants" onClick="javascript:KeepTotal();" multiple>
  12.    <option value="1">Name 1</option>
  13.    <option value="2">Name 2</option>
  14.    ... (about 150 names in total populated from a db)
  15. </select>
  16.  
as you can see i moved var i outside the function (declares it as global so it keeps its value) then i set it to 0 var i = 0; because your adding numbers, i changed it from i = i + 1 to i += 1 which is just cleaner looking and there you have it my friend.
May 11 '07 #2
pbmods
5,821 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. var i = 0;
  3. function KeepTotal(){
  4.    ++i;
  5.    if (i > 40) {
  6.       alert('You can only select 40 names');
  7.          return false;
  8.    }
  9.    return true;
  10. }
  11. </script>
  12. <select name="participants" onClick="return KeepTotal();" multiple>
  13.    <option value="1">Name 1</option>
  14.    <option value="2">Name 2</option>
  15.    ... (about 150 names in total populated from a db)
  16. </select>
  17.  
Made a couple of extra changes:
  • ++i because I can :)
  • KeepTotal returns true if OK to keep clicking, false if not.
  • the select's onclick s/b return KeepTotal to prevent the select from registering the click if the User has exceeded his quota.
  • onclick="javascript: is redundant.
May 11 '07 #3
iam_clint
1,208 Expert 1GB
nice catches your good! :P

keep up the good work pbmods
May 12 '07 #4
thats guys that worked like a champ there are still a few bugs i have noticed though.

1. if you happen to go over 40 names you get the alert but then if you de-select the 41st name you get the alert again and every time thereafter wether selecting or de-selecting.

2. is it possible when a user clicks the 40st name after the alert box to deselect the name they just selected as the 41st and subtract it from the counter so it will not keep displaying the message?

thanks allot for the help and the explanations.

-John
May 14 '07 #5
iam_clint
1,208 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1.  
  2. <script>
  3. var i = 0;
  4. function KeepTotal(obj){
  5.    var opts = obj.options;
  6.    var b = 0;
  7.    for (i=0; i<opts.length; i++)   {
  8.           if (opts[i].selected == true) { b++; }
  9.           if (b>40) { opts[i].selected = false; }
  10.    }
  11.   if (b>40) { alert("some options have been deselected! you may not have over 40 selected."); return false; }
  12. else { return true; }
  13.  
  14. }
  15. </script>
  16. <select name="participants" onClick="return KeepTotal(this);" multiple>
  17.  
May 14 '07 #6

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

Similar topics

6
by: David List | last post by:
I'm having a problem using different properties of the document object in the example javascripts in my textbook with browsers that identify themselves as using the Mozilla engine. One example of...
1
by: Covad | last post by:
Hi all, For some reason my change() function is only called when the page loads. I'd much rather it gets called when the select changes. Here's the code: window.onload = init; function...
3
by: ReGenesis0 | last post by:
I'm trying to rewrite the existing window using javascript. This works fine in IE- but in Mozilla, after I call a window.open, Mozilla starts wandering around like an alshiemers patient and...
1
by: jarit | last post by:
Hi, Found these coding guidelines for C#, HTML, Javascript, Java, HTML, PL/SQL, T-SQL, VB and VBScript. Well written and free to download. www.demachina.com/products/swat Jeroen
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
16
by: Brian D | last post by:
I have a multiple select list that is created dynamically based on a previous selection on an asp page. The first thing I do is to clear the curent option list by ...
1
by: Sideswipe | last post by:
I am trying to concatenate selected values from numerous radio button sets into 1 series and assign it to a single hidden field. I have never done javascript before and I continue to run into...
18
by: Andrew Wan | last post by:
I have been developing web applications with ASP & Javascript for a long time. I have been using Visual Studio 2003.NET. While VS2003 is okay for intellisense of ASP & Javascript, it's still not...
3
by: monojohnny | last post by:
Hi, I know you can do stuff with introspection to gather up passed-in args for a Javascript function and that you can list all defined functions like: function a(abc,xyz,zzz) {...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.