function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tyler HarrisTyler Harris 

Javascript Target an ID in Visualforce?

I've got my setSSKU() function setup to fire on the Model field, but I can't get my code to set the values on the Model and SKU(
<apex:inputField id="SSKU" value="{!objCase.Sensor_SKU__c}"/>
) fields. This code workw the the <apex:form> tag not sure why it's not working on this page.

JS
function setSSKU(){ var SSKUE = document.getElementById('{!$Component.pbMainLumidigm:SSKU}'); var model = document.getElementById('{!$Component.pbMainLumidigm:LuModel}').value; if(model=='M-Series'){ for(i=0; i < SSKUE.options.length; i++){ if(SSKUE.options[i].text.indexOf('V') >= 0){ SSKUE.options[i].style.display = 'none'; } if(SSKUE.options[i].text.indexOf('M') >= 0){ if(SSKUE.options[i].style.display == 'none'){ SSKUE.options[i].style.display ='block'; } } } } if(model=='V-Series'){ for(i=0; i < SSKUE.options.length; i++){ if(SSKUE.options[i].text.indexOf('M') >= 0){ SSKUE.options[i].style.display = 'none'; } if(SSKUE.options[i].text.indexOf('V')>=0){ if(SSKUE.options[i].style.display =='none'){ SSKUE.options[i].style.display = 'block'; } } } } }




Visualforce
<apex:pageBlock id="pbMainLumidigm" title="Case Edit" rendered="{!IsLumidigmRecType}"> <apex:pageBlockButtons > <apex:commandButton value="Save" action="{!saveCase}" onclick="return validateChannelPartner();"/> <apex:commandButton value="Save & Close" action="{!saveAndCloseCase}" onclick="return validateChannelPartner();"/> <apex:commandButton value="Cancel" immediate="true" action="{!cancelCase}"/> </apex:pageBlockButtons> <apex:pageBlockSection id="pbsFirst" title="Case Information" > <apex:pageBlockSectionItem > <apex:outputLabel value="Parent Case"></apex:outputLabel> <apex:inputField value="{!objCase.ParentId}"/> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlockSection> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="Product"/> <apex:inputField required="true" value="{!objCase.Product__c}"/> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="Model"></apex:outputLabel> <apex:inputField id="LuModel" required="true" value="{!objCase.Model__c}" onchange="setSSKU()"/> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > </apex:pageBlockSectionItem> <apex:pageBlockSectionItem rendered="{!isRCNHRecType || ispivCLASS}"> <apex:outputLabel value="OS Language"/> <apex:inputField value="{!objCase.OS_Language__c}"/> </apex:pageBlock>

 
Best Answer chosen by Tyler Harris
KevinPKevinP
Unfortunately, I don't think using javascript to set the values of these form fields will work. I'd suggest using an action fuction to collect your js parametrers and use something like this code, which I lifted from here: http://salesforce.stackexchange.com/questions/24666/how-to-pass-javascript-value-to-controller (http://salesforce.stackexchange.com/questions/24666/how-to-pass-javascript-value-to-controller" target="_blank)
 
<!-- A new value will be set to the apex:param -->
<apex:actionFunction name="passStringToController" action="{!myMethod}" rerender="myHiddenField">
    <apex:param name="p1" value="" assignTo="{!myString}" />
</apex:actionFunction>

<!-- Here we can directly access the action function per name and assign a variable value -->
<apex:commandButton value="Test me" onclick="passStringToController('new value'); return false;" />

 

All Answers

KevinPKevinP
Unfortunately, I don't think using javascript to set the values of these form fields will work. I'd suggest using an action fuction to collect your js parametrers and use something like this code, which I lifted from here: http://salesforce.stackexchange.com/questions/24666/how-to-pass-javascript-value-to-controller (http://salesforce.stackexchange.com/questions/24666/how-to-pass-javascript-value-to-controller" target="_blank)
 
<!-- A new value will be set to the apex:param -->
<apex:actionFunction name="passStringToController" action="{!myMethod}" rerender="myHiddenField">
    <apex:param name="p1" value="" assignTo="{!myString}" />
</apex:actionFunction>

<!-- Here we can directly access the action function per name and assign a variable value -->
<apex:commandButton value="Test me" onclick="passStringToController('new value'); return false;" />

 
This was selected as the best answer
rotfilrotfil
How rubbish is Salesforce? How can you not be able to reliably target an ID or name with Javascript?