1

I have two command buttons like "save" and "delete". If i click the save button then the "delete" button should disabled.

vf

<apex:page >
<apex:form >
<script type="text/javascript">
function dispalydivfun()
{
    alert("inside");
    document.getElementById('bbb').disabled=true;
          return false;
}
</script>

<apex:pageblock id="sd" >
<apex:pageBlockButtons location="TOP" >
<apex:commandButton value="save" onclick="return dispalydivfun()" id="aaa" />
<apex:commandButton value="delete" id="bbb" />
</apex:pageBlockButtons>
</apex:pageblock>   
</apex:form>
</apex:page>

3 Answers 3

2

You cannot disable an apex command button via id like this in javascript, because the id you set is rework by salesforce to generate a longer one, like this for instance : j_id0:form:j_id59:bbb. One way to get acces to the generated id is to use the $component global variable.

 <script>
   function disableThis(eid)
     {

        var but = document.getElementById(eid);
        but.disabled = true;
      }
  </script>

 <apex:pageblock id="sd" >
 <apex:pageBlockButtons location="TOP" >
 <apex:commandButton value="save"  onclick="disableThis('{!$component.bbb}')" id="aaa" />

 <apex:commandButton value="delete" id="bbb" />

Here i'm using pure javascript. You can use jquery also, this is not the problem. But one thing, you should keep in mind, even if the button is disable, you won't notice it visually, as salesforce use another style class on disabled button ("btndisable"). So more than just disabling it via javascript, you need to add the "btndisable" style class via javascript / jquery also.

1

Bit unclear why you're using command buttons without an Apex action.

You could save yourself the hassle and use plain html buttons in that case.

OR: you could use the Visualforce commandButton disabled attribute to stick to VF and not mess around with Javascript.

0

Can you use JQuery?

Your code with Jquery:

<apex:page >
<apex:form >
<script type="text/javascript">
function dispalydivfun()
{
    alert("inside");
    $('id$=bbb').prop( "disabled", true );
}
</script>

<apex:pageblock id="sd" >
<apex:pageBlockButtons location="TOP" >
<apex:commandButton value="save" onclick="return dispalydivfun()" id="aaa" />
<apex:commandButton value="delete" id="bbb" />
</apex:pageBlockButtons>
</apex:pageblock>   
</apex:form>
</apex:page>
5
  • I tried this.but it is not working ! it just refreshed.could you please suggest me another solution ?
    – Mahi
    Sep 10, 2015 at 9:12
  • Why you say that it doesn's work? In the comandbutton save the onclick should be onclick="displaydivfun()"
    – Torsen
    Sep 10, 2015 at 9:15
  • I tried but "delete" button is not disabled
    – Mahi
    Sep 10, 2015 at 9:21
  • What version of jquery are you ussing?
    – Torsen
    Sep 10, 2015 at 9:27
  • i am not using any jquery plugin
    – Mahi
    Sep 10, 2015 at 9:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .