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
SubhamSubham 

Make a button click on pressing enter button

Hey guys,

 

There is a scenario where there is a Input text file where i put any string for search

 

<td id="inputFilterBox" ><apex:inputText value="{!HIII}" id="inputtext" onclick="clearinputtext(this);"/></td>

   And then i click on Search button and it returns me the result. but the requirement is that if user presses enter , Search  button should be clicked or importantly Result should be displayed.

 

<input type="button" id="searchButton"  value="Search" onClick="search(); return false;"  />
Best Answer chosen by Admin (Salesforce Developers) 
Teach_me_howTeach_me_how

try this. credits to sandersen

 

<apex:form id="searchForm">
<script type="text/javascript">
  function noenter(e){
    if(window.event){
    key = window.event.keyCode; //IE
    } else{
    key = e.which; //firefox
    }
    if(key == 13) {
    var ele=document.getElementById('contactMergePage:searchForm:searchButton');
ele.click();
    return false;
    } else{
    return true;
    }
   }
</script>
<apex:inputText value="{!searchText}" id="searchText" onkeypress="return noenter(event);"/>
<apex:commandButton id="searchButton" action="{!search}" rerender="searchresults,selectedContacts,winnerpanel" value="Search" status="searchStatus"/>
<apex:actionStatus startText="(searching...)" id="searchStatus"/>
</apex:form>

All Answers

Teach_me_howTeach_me_how

try this. credits to sandersen

 

<apex:form id="searchForm">
<script type="text/javascript">
  function noenter(e){
    if(window.event){
    key = window.event.keyCode; //IE
    } else{
    key = e.which; //firefox
    }
    if(key == 13) {
    var ele=document.getElementById('contactMergePage:searchForm:searchButton');
ele.click();
    return false;
    } else{
    return true;
    }
   }
</script>
<apex:inputText value="{!searchText}" id="searchText" onkeypress="return noenter(event);"/>
<apex:commandButton id="searchButton" action="{!search}" rerender="searchresults,selectedContacts,winnerpanel" value="Search" status="searchStatus"/>
<apex:actionStatus startText="(searching...)" id="searchStatus"/>
</apex:form>

This was selected as the best answer
SubhamSubham

Thanks for teaching me how to do this:!!!!

 

 

CrocketCrocket

How are you able to avoid the embedded id information provided by salesforce. You know, the somewhat random "...j_id153:MyRealId"?

Manasa GuptaManasa Gupta

Hi,

 

I have a similar requirement.

i have 3 buttons, previous, reset and submit and i need to make submit button my default button i.e., on click of enter submit should be clicked.

 

when i tried your code, it didnt click the submit button instead Previous is getting clicked (Previous is the frist button so i assume that is why it is getting clicked).

 

will it matter if we have multiple button on the page?

also at what level should should we call the javascript?

Teach_me_howTeach_me_how

Are you sure that the document.getElementById that you call in the javascript is the id of the submit button?

Manasa GuptaManasa Gupta

yes..

 

<

 

b/>

<apex:commandButtonid="submit"value="Submit"action="{!addDetails}"onClick="return alert1();"/>

 

Also, getelement by id is returning null.

var ele=document.getElementById('submit');

alert(ele); 



Please let me know if i m doing anything wrong.

Manasa GuptaManasa Gupta

button was not getting clicked as I was trying on firefox.

 

To click a button on firefox use the soln in the below link:

 

http://developer.force.com/cookbook/recipe/submit-a-form-with-the-enter-key