Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I was able to get the id of the button but no able to get the outputText id. Please help

  <script >

  function exButton(){

   var remainingValues = document.getElementById("j_id0:j_id5:remaining").value;

    if(remainingValues > 0){
    document.getElementById("j_id0:j_id5:btn").click();
     }

    }
    function period(){

    setInterval("exButton()",50000)

    }

    </script> 

Apex button//

  <apex:commandButton id="btn" action="{!CreatePlan}" reRender="CreatePlan, Debug" value="Request Values" status="CreatePlanStatus"  onClick="period();" />

Here is the outputfield

    <apex:outputPanel id="Debug" layout="block">
       <h2>Remaining Values</h2><br/>
      <apex:outputText id="remaining" value="{!Debug}"/>
    </apex:outputPanel>
share|improve this question
    
Can you describe what this code's behavior is supposed to be and why you're using this pattern? It looks like you first click the button, start a timer which runs the exButton function every 50 seconds, at the time of the button click a partial page postback occurs and it rerenders the panel which contains the outputText data. Every 50 seconds the exButton function runs again and if the value of the outputText is greater than zero, you start yet another timer by clicking the button again and causing another partial postback and a rerender. Is this correct? –  Mark Pond Apr 16 at 21:51
    
You might consider using the <apex:actionPoller > tag if you can. It'll probably simplify the behavior of the page and you won't have to worry about the element ID values in the page before (or after) the rerender takes place. –  Mark Pond Apr 16 at 22:01
    
Mark, that is exactly what the script is doing. Thank you for the tip. –  Carlos Apr 16 at 23:30
add comment

2 Answers 2

up vote 6 down vote accepted

Visualforce takes over element Ids and often alters them. Have you checked the actual Id that is being rendered by Visualforce on your button? You may need to get creative with your Javascript to select your button.

EDIT: One common way to deal with this Id issue is to use the jQuery EndsWith() selector:

j$('[id$="btn"]');
share|improve this answer
    
Hi Greenstork, how can I see the actual id that is bein rendered. any ideas? –  Carlos Apr 14 at 23:46
    
Right click on the button and inspect the element using Chrome or Firefox. –  greenstork Apr 14 at 23:57
    
This is the ID I am getting id="j_id0:j_id5:btn", is that right? –  Carlos Apr 15 at 0:09
    
That's exactly what I was talking about. Visualforce takes over your Id. You can use jQuery to select the id using the EndsWith() selector. j$('[id$="btn"]'); –  greenstork Apr 15 at 0:14
    
Thank you so much! I used the Id above and it works. Appreciate it! –  Carlos Apr 15 at 2:21
show 6 more comments

You can put alert("xxx") into the function of period and exButton.then You can find out where problem is.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.