Button deactivation

Hi, I’ve managed to figure most things out along the way however, this is giving mesome trouble as I’ve not done any sort of coding since 2009. I am trying to make my “Accept Job” button clickable ONLY 5 times and then it needs to disable. The member can ONLY click the button once.

The “Decline button” I need to stay active on the page but, I need it to disable for the individual member who declines the job.

I’ve added an image to show what the DEV mode code is showing and the 2 buttons I’m talking about. If someone could write the code for me, that would be great! If it’s a chargeable thing, this is also fine. The page is a dynamic page which pulls through the info added by a customer. It has a small amount of info on the list page which is then linked to the item page (where these buttons are located), the “Accept Button” then links to a customer details page which I only want 5 seperate members to obtain as I don’t want the customers being bombarded with loads of emails/texts/calls etc.

Thanks for any info, help solving my issue in advance

That shouldn’t be a big deal.

I will come back to you in few hours…

Hi Code-Ninja,

Thanks for getting back to me!

I look forward to hearing from you.

Ok, let’s say you gave your accept-button the following ID → " btnAccept "

Delete all your code on your page and replace it with the following one…
Publish your website and test it.
Do not forget to to change the ID of your acceptButton, like shown in the example.

$w.onReady(()=>{let counter = 0;
	let btnAccept = $w('#btnAccept');
	btnAccept.onClick((event)=>{
		console.log(event.target.id+"-clicked-"+counter);
		conter = counter +1;		
		if (counter >=5) {btnAccept.disable();}
		else {btnAccept.enable();}
	});
});

Hi Code-Ninja,

I’ve changed my button ID as advised. Deleted everything else and input your code. It’s not coming up with any errors although, when I click the button, the registry of the “count” is continuosly “0” and isn’t counting up.

Does it have to be on a “published site” or can it be tested via this DEV area? Sorry, I can do most things but, coding isn’t a strong point and confuses me.

Thanks for your help.

I’ve just tried it Live and it’s still allowing the clicks…

conter = counter +1; → little typo —> wrong

counter = counter +1; —> right

Correct this typo and normaly it should work.

Hi, I did reply yesterday but, it doesn’t seem to have come through. I changed that and still not getting the deactivation after 5 clicks. Do I need to add the —> as I’ve not done that…

The following code was tested… and works like it should…

$w.onReady(()=>{let counter = 0;
	let btnAccept = $w('#btnCvCreationTool');
	btnAccept.onClick((event)=>{
		console.log(event.target.id+"-clicked-"+counter);
		counter = counter +1;		
		if (counter >=5) {btnAccept.disable();}
		else {btnAccept.enable();}
	});
});

As we can see inside the console, i was able to click only 5-times onto the button, until the button had been deactivated…

That was my testing button, somewhere on one of my running projects…

After 5-clicks it got disabled…

Of course your button will be REENABLED again, if you …
---------------> REFRESH THE PAGE <--------------

And now you should think about a database → which saves all the clicks of each users, who visits your site.

  1. User-A visit your site and clicks 4-times, before leaving your page.

  2. User-B visit your site and clicks 3-times, before leaving your page.

  3. User-C visit your site and clicks 5-times, before leaving your page.

All of this users, would automaticaly refresh the button, and the button would work again and again, if you do not save the current state of the button for each of the users.

But how to do? What do you need to achieve such functionality?

  1. First you should be able to get the ID of current user.

  2. You should be able to get the amount of clicks of current user.

  3. You should be able to save the current amount of clicks to your DB for each user.

  4. You should be able to load the data for each user automatically when the page gets ready (including your BUTTON).

What would be the code-flow…

  1. User logs in —> you get his ID

  2. Now when you know users ID → loading the data of this user including the AMOUNT of CLICKS, from your DATABASE.

  3. Running an IF-ELSE-CONDITION to check if the AMOUNT is smaller, equal, or even bigger than 5.

  4. if (…) {…} else {…}

  5. Accordingly disable or enable the button.

  6. At same time after every click onto your button you should save the current amount of clicks back to your database.

This would make sure, that the clicked button-amount never would get lost, since it is saved inside your database for each of users.

Now your turn —> generate all the functions and solve your issue.

EDIT: ADDITIONAL-TIP:

If you want to know if the code has any errors or TYPOS, just paste the code into your Wix-Editor into the code-section and all the ERRORS will be marked → RED!


As we can see —> conter <— was a real TYPO !!!
Instead of → conter ← of course it should be —> counter <—

Now you should have enough informations, to be able to solve your issue by your own.