1

This should be easy! I just cant think how to do it.

I have a couple of fields and a hidden field. I want do the following with the hidden field "field 1 - field 2"

I just can't think how to pass these 2 values over. Javascript isnt really an option I dont think as the id's of these fields are set by the SF server when the DOM loads, it seems to give :jd0:jd1.... etc.

I essentially need del.Name = del.name__c + '-' + selectedType

My fields are as follows:

<apex:selectList value="{!selectedType}" multiselect="false" size="1" id="eventtype"></apex:selectList>
<apex:inputField value="{!del.Name__c}" id="delname"></apex:inputField>

<apex:inputHidden value="{!del.Name}" id="recname"></apex:inputHidden>

1 Answer 1

2

Yes, you'd use JavaScript, and yes, you'll have to deal with the managed ID values somehow. One reasonable trick is to not rely on the ID, but instead something else you can query.

<apex:selectList html-data-selList="1" onchange="updatehidden()" ...
<apex:inputField html-data-inpField="1" onchange="updatehidden()" ...
<apex:inputHidden html-data-inpHidden="1" ...

Your function can then query for the custom attributes:

function updatehidden() {
  var selList = document.querySelector("[data-selList]"),
    inpField = document.querySelector("[data-inpField]"),
    inpHidden = document.querySelector("[data-inpHidden]");
    ...
}

Of course, this isn't the only way; many people use custom attributes or custom style classes, which you can also query on. It's also possible to query based on a partial ID, too:

<apex:selectList id="sellist" onchange="updatehidden()" ...
<apex:inputField id="inpfield" onchange="updatehidden()" ...
<apex:inputHidden id="inphidden" ...


var sellist = document.querySelector("[id*=sellist]");
var inpfield = document.querySelector("[id*=inpfield]");
var inphidden = document.querySelector("[id*=inphidden]");

Self-Contained Example:

<apex:page standardController="Account">
    <script>
    function updatehidden() {
        var selList = document.querySelector("[data-selList]").value,
            inpField = document.querySelector("[data-inpField]").value,
            inpHidden = document.querySelector("[data-inpHidden]");
        inpHidden.value = selList + ' - ' + inpField; 
    }
    </script>
    <apex:form>
        <apex:selectList html-data-selList="1" size="1" id="sellist" onchange="updatehidden()">
            <apex:selectOption itemLabel="value 1" itemValue="value 1" />
            <apex:selectOption itemLabel="value 2" itemValue="value 2" />
            <apex:selectOption itemLabel="value 3" itemValue="value 3" />
        </apex:selectList>
        <apex:inputField html-data-inpField="1" id="inpfield" onchange="updatehidden()" value="{!Account.Name}" />
        <apex:inputHidden html-data-inpHidden="1" />

    </apex:form>
</apex:page>
5
  • I thought I could use the following but it doesnt seem to work... <script> function updatehidden() { var selList = document.querySelector("[data-selList]").value, inpField = document.querySelector("[data-inpField]").value, inpHidden = document.querySelector("[data-inpHidden]").value; inpHidden = selList + ' - ' + inpField; </script>
    – Joe Hayes
    May 30, 2018 at 14:49
  • Looking in the console I'm getting "Cannot set property 'value' of null" on the inpHidden.
    – Joe Hayes
    May 30, 2018 at 15:03
  • 1
    @JoeHayes See my self-contained example. You had a bit of a mix-up with inpHidden line and the line after.
    – sfdcfox
    May 30, 2018 at 15:20
  • Thanks for this, I still can't get it to work, I added some console.log() so I could see what was going on and it displays fine if I remove the reference to inphidden. It seems that as soon as it tries to read or set inphidden it errors and says cannot set/read value of null.
    – Joe Hayes
    May 30, 2018 at 15:50
  • I've figured it! The InputHidden needed to be inside an apex:column. I didnt notice this. Got it all working now! Thanks so much for all your help
    – Joe Hayes
    May 30, 2018 at 16:01

You must log in to answer this question.

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