function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MagnetismMagnetism 

Disable a button onClick that calls javascript function from where @RemoteAction method in apex Controller is invoked

Disable a button onClick that calls javascript function from where @RemoteAction method in apex Controller is invoked

I am trying to disable a button on meeting a condition. This <Button> helps me invoke a RemoteAction function in VF Controller. Need help in doing this. suggestion and solutions are appreciated.
Thanks

Note: This is not apex command button
Best Answer chosen by Magnetism
Avidev9Avidev9
You can use a command button to invoke your JS / Remote action. Something like

<apex:commandButton value="Invoke My Remoting Method" onclick="remoteMethod();return false;" />

Now when you do this you can use the disabled attribute.

<apex:commandButton value="Invoke My Remoting Method" onclick="remoteMethod();return false;"  disabled="true"/>


All Answers

AshlekhAshlekh
Hi,

You can use below script to disable and enable button.
CallMEOnclick()
{
	var clickedButtons = new Array();
	var buttons = document.getElementsByTagName("input");
	for(var i = 0; i < buttons.length; i++)
	{
		if(buttons[i].type == <input or submit provide value>  || buttons[i].type == "button")
		{
			if(buttons[i].value == <value of your button>)
			{
				clickedButtons.push(buttons[i]);
			}
		}
	}
	//Want to disable a button 	call me
		disableButtons();
		
	//Call me when you want enable the button	
}
 function disableButtons(ele)
{
	for(var i = 0; i < ele.length; i++)
	{
	ele[i].disabled = true;
	ele[i].className = "btnDisabled";
	ele[i].value = "Processing...";
	}
};

function enableButtons(ele)
{
	for(var i = 0; i < ele.length; i++)
	{
	ele[i].disabled = false;
	ele[i].className = "btn";
	ele[i].value = "your button value";
	}
};

Please mark it a solution if it helps you and ENJOY APEX.
Avidev9Avidev9
You can use a command button to invoke your JS / Remote action. Something like

<apex:commandButton value="Invoke My Remoting Method" onclick="remoteMethod();return false;" />

Now when you do this you can use the disabled attribute.

<apex:commandButton value="Invoke My Remoting Method" onclick="remoteMethod();return false;"  disabled="true"/>


This was selected as the best answer
MagnetismMagnetism
Thanks For your answers guys. 

Avidev9: Your answer worked for me. But I am curious why you have return false; after the remoteMethod();
Avidev9Avidev9
So by default a command button submits the form. Adding the return false stops any further action after onclick event and eventually form is not submitted. I this case we just wanted the onclick.