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
Hugo007Hugo007 

AJAX and ENTER key handling

Hi all,
 
I am stuck with something that I thought was simple but yet it does not wok!
 
When I try the code below, I expect that when I press ENTER, it does the same as clicking on the addBtn Button. But it does not. Calling .click() only calls the onclick event of the addBtn. The oncomplete is not called. And therefore, my jsAddItem action function is not called.
 
When I use the javascript console and I call the .click() from there, then it works.  By the way, this only happens when using AJAX: when the rerender is specified on the actionFunction. 
 
Anyone has an idea of what is going on?
 
 
Visual Force page excerpt:
        <apex:form >
            <apex:actionFunction action="{!AddItem}" name="jsAddItem" rerender="pbitems,jspanel">
                <apex:param name="Hostname" value="" assignTo="{!Hostname}"/>
            </apex:actionFunction>
            <apex:outputPanel rendered="{!NOT(Box__c.Shipped__c)}">
                Type or scan an item identifier (SN, PN, Box Name):&nbsp;
                <apex:inputText value="{!inputValue}" id="theInputId" onkeyup="doAddOnEnter(event);" />
                <apex:commandButton id="addBtn" value="Add" onclick="console.log('Calling onclick');" oncomplete="try{console.log('Calling jsAddItem()');jsAddItem('myhost');} catch(err){ShowError(err.message);}console.log('DONE');" />
            </apex:outputPanel>
<script>
function doAddOnEnter(e){
 var keynum = 0;
 if (window.event) {
 keynum = window.event.keyCode;
 }else if(e.which){
 keynum = e.which;
 } else {
 keynum = event.keyCode
 }
 if (keynum == 13) {
 var button = document.getElementById('{!$Component.addBtn}');
 button.click();
 }
}
</script>            
 </apex:form >
 
 
 
 
Javascript console output when pressing enter while the textbox has focus:
Calling onclick AddItemsToBox:1
 
Javascript console output when calling manually:
document.getElementById('j_id0:pbitems:j_id77:addBtn').click();
Calling onclick AddItemsToBox:1
undefined
Calling jsAddItem()
DONE

 

Best Answer chosen by Admin (Salesforce Developers) 
Hugo007Hugo007

Found the anser by searching a bit more on the forums. Thanks to