[SalesForce] Using JQuery to conditionally hide buttons on Quote Page

I was following Tehnrd's tutorial (http://www.tehnrd.com/show-and-hide-buttons-on-page-layouts/) on how to conditionally hide/show custom buttons on a page layout. I think Tehnrd's code deals with just fields that are text base, however, I want to use a checkbox field for the condition. I have put "false" in the field value and the button was hidden from the page as expected, but when I flipped it to true, the button did not appear. I tried creating this jquery code in the html area component, but that did not have success either. Do I have to change the j$().text to fit with a checkbox?

<!-- Import jQuery from google CDN, could also be static resource--><script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script><script type="text/javascript">
var j$ = jQuery.noConflict();

var objectPefix = window.location.href.substring( window.location.href.indexOf('.com/') + 5, window.location.href.indexOf('.com/') + 8 );

/*Query the records from Dynamic_Button__c object as soon as possible, don't wait for DOM ready event*/
var result = sforce.connection.query("Select Button_Name__c, Field_Id__c, Field_Value__c from Dynamic_Button__c where Object_Prefix__c = '" + objectPefix + "'");
var records = result.getArray("records");

/*Execute this code block once page DOM has fully loaded*/
j$(document).ready(function(){
    /*Hide the sidebar last as this is lowest priority. First priority is show/hiding the buttons.
    Find the sidebarComponentLocator, then find parent div with class 'sidebarModule' and the hide it*/
    j$("#sidebarComponentLocator").closest(".sidebarModule").hide().prev().hide(); 

    /*First loop through the dynamic button records and hide any that are on the layout. We must first hide all the buttons
    as the Dynamic Button records only contan the 'show' logic*/
    for (var quoterecordValue = j$("#00NK0000001L8ub_ileinner").children().attr('title');

    /*Hide button if Submit for Approval Field is not 'False'
    if(quoterecordValue != 'false'){
        j$("input[name='add_quote_products']").hide();
    }
    */
});

    /*Now loop through the Dynamic_Button_Records and show button if field value matches that define in record*/
    for (var i = 0; i< records.length; i++) {
        /*Get the value from the field on the page layout*/
        var quoterecordValue = j$("#00NK0000001L8ub _ileinner").children().attr('title');
        var showValue = records[i].Field_Value__c;

        /*Show the button if the value of the field on this record matchs the setting in the Dyamic_Button__c*/
        if(quoterecordValue == showValue){
            j$("input[name='add_quote_products']").show();
        }
    }

</script>

Best Answer

Do I have to change the j$().text to fit with a checkbox?

This is correct. I went and started to look at the HTML for checkbox and saw it is in fact an image that they are using. What I suggest is to read an attribute from that image such as alt or title as you can see how the HTML looks below

<div id="00N50000003JWbq_ileinner"> <img src="/img/checkbox_checked.gif" alt="Checked" width="21" height="16" class="checkImg" id="00N50000003JWbq_chkbox" title="Checked"> </div>

This would be the javascript I would have to use to get the value "Checked" as shown below:

j$("#00N50000003JWbq_chkbox").attr('title');

What I'm doing here in the javascript / jquery, is I find the section of html which is holding on the precious information I want to extract, and since it has a child record, I'm using the children() function to go a step deeper inside the html as this is nested within your div container. Lastly, I use the attr() method which allows me to call any attribute as seen above in the html image tag to retrieve a value.

Please remember to use the correct ID as this was an example from my side. Lastly, I would also recommend checking to see if your developer console / firebug / trouble shooting application tells you if you have any errors via javascript.