3

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>
3
  • 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
    Commented Apr 16, 2014 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
    Commented Apr 16, 2014 at 22:01
  • Mark, that is exactly what the script is doing. Thank you for the tip.
    – Carlos
    Commented Apr 16, 2014 at 23:30

2 Answers 2

6

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"]');
11
  • Hi Greenstork, how can I see the actual id that is bein rendered. any ideas?
    – Carlos
    Commented Apr 14, 2014 at 23:46
  • Right click on the button and inspect the element using Chrome or Firefox.
    – greenstork
    Commented Apr 14, 2014 at 23:57
  • This is the ID I am getting id="j_id0:j_id5:btn", is that right?
    – Carlos
    Commented Apr 15, 2014 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
    Commented Apr 15, 2014 at 0:14
  • Thank you so much! I used the Id above and it works. Appreciate it!
    – Carlos
    Commented Apr 15, 2014 at 2:21
1

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

You must log in to answer this question.

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