If you're looking to have the inputfield
hidden on page load, you can add the CSS class 'hidden' to the tag directly. Salesforce pages all have this class defined within their stylesheets.
<apex:page id="pageid" standardcontroller="account">
<apex:form id="formid">
<apex:pageblock id="pbid">
<apex:pageblocksection id="pbsid">
<apex:inputfield id="phoneid" value="{!account.phone}" styleClass="hidden" />
</apex:pageblocksection>
</apex:pageblock>
</apex:form>
</apex:page>
If you want to ensure future maintainability, you should define your own hidden
class within your markup. This will prevent future platform stylesheet changes from changing the behavior of your solution.
<apex:page id="pageid" standardcontroller="account">
<style type="text/css">
.hidden { display: none; }
</style>
<apex:form id="formid">
<apex:pageblock id="pbid">
<apex:pageblocksection id="pbsid">
<apex:inputfield id="phoneid" value="{!account.phone}" styleClass="hidden" />
</apex:pageblocksection>
</apex:pageblock>
</apex:form>
</apex:page>
If you'd like to hide it with script you can use markup like this, leveraging the $Component
global to dynamically retrieve the ID of your inputField
in the page hierarchy. Be aware that using this method will allow the inputField
to be visible in the browser until the script executes and hides it.
The CSS method above will hide the field as the page is rendered, where the script method will hide it after the script runs. Allowing the user to see the field, even temporarily, isn't the optimal implementation.
<apex:page id="pageid" standardcontroller="account">
<apex:form id="formid">
<apex:pageblock id="pbid">
<apex:pageblocksection id="pbsid">
<apex:inputfield id="phoneid" value="{!account.phone}" />
</apex:pageblocksection>
</apex:pageblock>
</apex:form>
<script>
document.getElementById('{!$Component.pageid.formid.pbid.pbsid.phoneid}').style.display = 'none';
</script>
</apex:page>
Documentation: Best Practices for Accessing Component IDs
When using and troubleshooting the $Component
markup, be sure to look at the rendered source code for the page in the browser. This should simplify the steps to figure out what's not correct in the VF markup.
If the path to your component is correct, the HTML will have the hierarchical ID in the rendered source. document.getElementById('pageid:formid:pbid:pbsid:phoneid').style.display = 'none';
If the path to the component was not correct and it couldn't be located using what you've provided, the rendered source code will have a blank value where you would expect to see the ID. Like this: document.getElementById('').style.display = 'none';