1

Type is a picklist and reports is a checkbox. When a certain picklist is selected I want the checkbox to be disabled. I tried using an if statement but it does not work and I'm not sure why. Can anyone help? Thanks.

VisualForce page

<apex:page standardController="Account" extensions="getItems">    
  <apex:outputPanel layout="block" style="overflow:auto;width:500;height:180px">
   <apex:form styleClass="formId">
       <apex:pageblock id="table">

        <script>
        function openWindowPopup(bid,rid){

                var typeVal = document.getElementById(bid).value;

                var Reports = document.getElementById(rid).checked;



                if(typeVal = "type1"){
                    Reports.disabled = true;
                }
                else{
                    Reports.disabled = false;
                    }


            window.open('https://www.local.com?type='+typeVal+'&rprts='+Reports);

        }                           
        </script>


 <apex:pageblocktable id="pageBlockId" value="{!Assetitems}" var="lines" >     
            <apex:column >
                <apex:commandbutton value="Generate" reRender="table" onclick="openWindowPopup('{!$Component.mybid}''{!$Component.myrid}')" />       
            </apex:column>

                <apex:column headerValue="Type">
                    <apex:inputField value="{!Asset.Type__c}" id="mybid"/>
                </apex:column> 


                <apex:column headerValue="Reports">
                    <apex:inputField value="{!Asset.Reports__c}" id="myrid"/> 
                </apex:column> 

            </apex:pageblocktable>
        </apex:pageblock>
    </apex:form>
</apex:outputPanel>

1
  • I think I have found all the issues - take a look Commented Jun 2, 2015 at 17:05

1 Answer 1

2

Javascript uses the = operator as an assignment operator and the == operator as an equality test. Change this:

if(typeVal = "type1"){

To this:

if(typeVal == "type1"){

And things should work better.

EDIT Also, add a comma between your argument list in the function call, ie,

onclick="openWindowPopup('{!$Component.mybid}','{!$Component.myrid}')"

Finally, you need to work out wether you want to send a true or false to the window you are opening or set the checkbox disabled.

To actually disable the checkbox, you would code Reports.disabled = true assuming that you had a reference to the checkbox component.

To send a true or false to the window, you would need a reportsDisabled variable that reflects the value of the checkbox disabled state. See the code below:

    var typeVal = document.getElementById(bid).value;
var Reports = document.getElementById(rid);
var ReportsChecked = Reports.checked;

if(typeVal == "type1"){
    Reports.disabled = true;
}
else{
    Reports.disabled = false;
}   
window.open('https://www.local.com?type='+typeVal+'&rprts='+Reports.disabled); 

I have verified this works - I replicated your page.

2
  • Hi CapNZ, Thank you for your very detailed answer I feel so silly missing the ==. However when I try your solution reports comes up as "undefined" in the url, which is ok for when the if statement is true but when its not it still comes up as undefined any idea why its doin this ? Thanks. Commented Jun 3, 2015 at 9:21
  • It's ok got it, I created a variable inside the if loop equal to reports.disabled and used it in the url. Thanks again for your help. Commented Jun 3, 2015 at 10:01

You must log in to answer this question.

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