I have a button:
<apex:commandButton action="{!pullReport}" value="Pull Report" id="pullReportbutton"/>
That refreshes the page every time it is clicked. If the page does not refresh, the data will not be returned. (IE: if I use reRender="none" or return= "false")
I have a JS function that will hide the button once it is clicked.
My issue is that once I click the button, the button is hidden for 2 seconds, then the page reloads, and the button is back.
Is there a way around this without having to restructure my apex:form or apex:commandButton? I have written conditionals to try and say "if there is data returned then hide the button" but these get refreshed as well. My last resort is to re-write the button in JS but I would prefer to not do that since I have it already written in visualforce.
some JS I've tried:
/*
$("#pullReportbutton").click(function(event){
event.stopPropagation();
alert("The span element was clicked.");
});
if("{!companyname}" != null){
$(function(){
document.getElementById('pullReportbutton').style.display = 'none';
});
}
function hidebaseObject(e){
e.preventDefault();
var elem = document.getElementById('pullReportbutton');
elem.parentNode.removeChild(elem);
}
*/
UPDATES: Full Code For Easier Viewing
<script type="text/javascript">>
if("{!companyname}" != null){
$(function(){
document.getElementById('pullReportbutton').style.display = 'none';
});
function hidebaseObject(e){
e.preventDefault();
var elem = document.getElementById('pullReportbutton');
elem.parentNode.removeChild(elem);
}
}
</script>
<apex:form id="mainForm">
<div class="col-md-6" style="position:relative">
<div>
Business ID Report
</div>
<div id="removeMePlease">
Instant ID Report has not yet been pulled for this organization.
</div>
<div>
<apex:commandButton action="{!pullReport}" value="Pull Report" id="pullReportbutton" reRender="mainForm" />
</div>
<div>
Business ID Report
</div>
<div>
Company Name: {!companyname}
</div>
<div>
Report Date
</div>
<div>
{!newDate}
</div>
<div>
Full Report
</div>
<div><a href="{!helloKitty}" target="blank">
View Details
</a>
</div>
</div>
</apex:form>
Best Answer
Ok, there are a couple of things going on here:
When I need that to happen I usually wrap the JS code inside an outPutPanel and rerender that when doing ajax actions:
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_best_practices_accessing_id.htm
However there is an easier solution:
Use jQuery and the endswith selector
$("[id$=pullReportbutton]").xxx
Example Controller
Example Page
I tried to use your markup whenever possible to illustrate thins as in your example
You will notice that I used the following points in the code, if you use jQuery you can stick to using the button ID to hide using the endswith selector.
So take what is above, it is a working example. Start making modifications to have it meet your needs, test, test, test, when it breaks you will know what modification broke it and then don't do that and find the correct way.
Update based on your comments
I would do it differently than above (I would also use jQuery) but here is a working example:
Code
VF Page