473,387 Members | 1,435 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,387 software developers and data experts.

Limit number of clicks

KeredDrahcir
426 256MB
I have a loop which draws a 12 x 8 grid of 96 squares. I'm using a JavaScript function so that if a user clicks on a square it changes to red and if they click on it again, it will change back to black.
What I want to do it prevent them from having more than 50 red squares at any one time.

I have a function that does this kind of think with checkboxes but I'm not sure how to do it with the grid I'm using. Can anyone help please?

JavaScript
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.   <!--
  3.     function plan(id){
  4.       var obj = document.getElementById(id);
  5.       obj.style.backgroundColor = (obj.style.backgroundColor == "rgb(0, 0, 0)") ? "#ff0000" : "#000000";
  6.      }
  7.   //-->
  8. </script>
Box (within loop)
Expand|Select|Wrap|Line Numbers
  1. <div style="width: 30px; height: 30px; border-style: solid; border-color: #ffffff; border-width: <?=$border;?>; float: left; background-color: #000000;" id="<?=$row, $column;?>" onclick="plan('<?=$row, $column;?>');">&nbsp;</div>
Aug 5 '13 #1
4 3866
Rabbit
12,516 Expert Mod 8TB
A couple of ways to do this. Either loop through your divs and keep a count of how many of them are red or keep a running count whenever the function is called and decrement or increment appropriately.
Aug 5 '13 #2
Exequiel
288 256MB
Try this one. .
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.       <!--
  3.       var count=1;
  4.         function plan(id){
  5.  
  6.           var obj = document.getElementById(id);
  7.           if(count <= 5){
  8.               obj.style.backgroundColor = (obj.style.backgroundColor == "rgb(0, 0, 0)") ? "#ff0000" : "#000000";
  9.           }else{
  10.                alert('You reach the maximum click.  Total click: '+count);
  11.         }
  12.  
  13.           count++;//counter
  14.  
  15.          }
  16.       //-->
  17.     </script>
Aug 6 '13 #3
Rabbit
12,516 Expert Mod 8TB
@exequiel, you need to start the count at 0. And you have no decrement. You need to decrement/increment appropriately otherwise the code stops working after any 5 clicks rather than after 5 red divs.
Aug 6 '13 #4
Exequiel
288 256MB
@Rabbit . okey, thank you for that comment. . really appreciated. . :)
Aug 6 '13 #5

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

Similar topics

29
by: Paul | last post by:
Hi, I'd like to limit the number of selections a user can make in a multiple select listbox. I have a note on the interface to say that only x no. of items should be selected and I check the...
2
by: Dalan | last post by:
I am finalizing an Access 97 db and need to decide was is the best method to limit the number of records one can enter in the demo version of same. The archives seem to be replete with many...
2
by: Dalan | last post by:
I have created a table and form for allowing the input of a unique customer number (not an ID autonumber number) which a customer would enter once after installing the database. I'm using DLookup...
2
by: Ian | last post by:
Using Access 97 I want to distribute my database over a LAN but limit the number of PC's that can use it at the same time. Perhaps limit it to 3 so when the 4th user tries to open it they cant...
3
by: TomislaW | last post by:
I need to show 5 rows and put link for more.. where and how to put this limit? on item data bound or else?
2
by: OM | last post by:
I have two tables, one with categories in it, and only 3 records - Wages, Salary, Contract. I have a second table that records hours and numbers of employees in each category, per month. How do...
1
by: jobs | last post by:
I've tried setting the height of the dropdownlist, but when I do that it eats of space below . Basically, I only want about 10 rows visible and scrolling to then show up if there are more rows. But...
6
by: Breana | last post by:
How can i limit the number of results to 10 and have a Next >> button appear or << Back on the bottom if there is more than 10 results? Because as of now it's only 7 members but it will soon be 400...
3
juve11
by: juve11 | last post by:
hello, i have a vb application that writes into mysql (some kind of autocomplete). problem is that it writes too many rows and the app at a moment starts to run slowly.so an option that i thaught...
3
by: neelsfer | last post by:
i want to limit the capturing in a subform to the first 10 records. Pls help mainform = racesetupf (table name =racesetup) fields = racesetupid(autono) racename(txt); racedate(short) Subform...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.