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'm trying to hide an inputfield on visualforce page based on some condition. So, i started with below code, but unable to hide the inputfield on page load itself. Both the alert messages displayed before and after the js code are executing well. but, the component is not hiding from the page(able to grey it out successfully with the below code, but not hiding it). Could not use rerender functionality, as my section contains rich text area., and rerender is not working for components containing rich text area. My code below:

    <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>
     alert('d2 opens now');
     try{
         document.getElementById("pageid:formid:pbid:pbsid:phoneid").visible=false;
         }catch(e){
         alert(e)
         }
         alert('d2 opens now1');
  </script>
</apex:page>

Thanks in advance, Friend!!!

share|improve this question
add comment

4 Answers

up vote -1 down vote accepted

Try this

document.getElementById("pageid:formid:pbid:pbsid:phoneid").style.display ='none';
share|improve this answer
add comment

In my opinion, using jQuery would be best and easiest, as you can simply do this:

$("[id$=phoneid]").hide();

This will not break if someone changes the markup of the page, since it just uses the id of the apex:inputField.

share|improve this answer
2  
Be aware that this selector will hide every element in the DOM which has an ID value that ends with phoneid. jQuery ends with selector documentation. If the inputField is defined within a repeating structure (repeat, table, etc.), it will hide every one of them. –  Mark Pond Jul 28 '13 at 20:09
    
Actually, it wouldn't work on a apex:repeat, because each row gets the id modified to be like phoneid:0, phoneid:1, etc. Using an ends-with would work only for non-repeated elements. –  sfdcfox Jul 28 '13 at 21:09
    
@sfdcfox your assertion is incorrect. In a repeated element situation, the repeater index comes prior to the tag ID value. repeatThing:0:phoneid, repeatThing:1:phoneid, repeatThing:2:phoneid, repeatThing:3:phoneid, etc. salesforce.com/us/developer/docs/pages/Content/… –  Mark Pond Jul 29 '13 at 1:54
    
You're correct. I think it used to be the other way around, though; I could have sworn that's the way it was before. Thanks for the correction. –  sfdcfox Jul 29 '13 at 2:17
    
Thanks @MarkPond I did not know that! –  Lex Jul 29 '13 at 7:08
add comment

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';

share|improve this answer
add comment

As Venkatesh said, using.style.display ='none' should be enough, but I see a problem that your layout marking is not flexible, because your js would be broken each time as you or somebody will make changes in layout. As you can see the id for <apex:inputfield component generated by adding ids of all outer component and adding new component to this hierarchy will broke your js. You can try to use html input field instead <apex:inputfield where id would be the same as you wrote in the code.

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.