+ Start a Discussion
Bill ZhuangBill Zhuang 

js change apex inputHidden value then post to controller, value missing

page:

 

<apex:form id ="myForm">
<apex:inputHidden value="{!value1}" id="theHiddenInput"/>
<apex:commandLink value="Save" action="{!save}"/>
<script>
document.getElementById('{!$Component.myForm.theHiddenInput}').value= "lalala";
</script>

 

controller:

public String value1{get;set;}
public PageReference save(){ system.debug('invalue1 ' + value1); return null; }

 

 

question:

when i try to submit this form about 30 times, i will meet the value1's value in log is empty 1 time, and also use fiddler to watch form data, also found the theHiddenInput's value is empty. why? a low percentage to lost the hiddeninput value? anyonu meet this issue before?

 

tks

 

Shiva Ramesh @ xcdhrShiva Ramesh @ xcdhr

check below code its works for me:

 

<script>
function updateHiddenValue() {
  v = document.getElementById('{!$Component.myHiddenElement}')
  v.value = 'Some new value'
}

if(window.attachEvent)
  window.attachEvent('onload',updateHiddenValue)
else if(window.addEventListener)
  window.addEventListener('load',updateHiddenValue,true)
</script>

<!-- somewhere else in your code -->
<apex:inputHidden id="myHiddenElement" value="{!myHiddenValue}"/>