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
Sushant RaoSushant Rao 

Triggering Javscript Events -- click();

I know that we can call the click() event of a button from Javascript like below ...but I can't access the click event of an inputText element.

 

Thanks for the help!!!!

 

var btnCopy = document.getElementById("{!$Component.btnCopy}");
btnCopy.click(

 

 

<apex:inputText onClick="a();" /> 
<apex:inputText id="zz" onClick="alert('hi');"/> 
       <script>
       function a() {
        document.getElementById("{!$Component.zz}").click();
       }
       </script> 

 

Best Answer chosen by Admin (Salesforce Developers) 
aballardaballard

Just tried it.  Turns out the DOM element name is "onclick" (all lowercase)  not "onClick".   It works if you change to

 

<apex:inputText value="{!Params.ContractName}"     onclick="document.getElementById('{!$Component.zz}').onclick();"  />                  

<apex:inputText value="{!contractName}"  id="zz" onclick="alert('hi');" />

All Answers

VRayVRay

When you want to do something like that, you must use <apex:actionsupport>... example below:

 

 

<!-- Page: -->
<apex:page controller="exampleCon">
    <apex:form>
      <apex:outputpanel id="counter">
         <apex:outputText value="Click Me!: {!count}"/>
         <apex:actionSupport event="onclick"
            action="{!incrementCounter}"
            rerender="counter" status="counterStatus"/>

      </apex:outputpanel>
      <apex:actionStatus id="counterStatus"
      startText=" (incrementing...)"
      stopText=" (done)"/>
   </apex:form>
</apex:page>

 

 

 

/*** Controller: ***/
public class exampleCon {
Integer count = 0;
public PageReference incrementCounter() {
count++;
return null;
}
public Integer getCount() {
return count;
}
}

 

 

hope that helps.

Sushant RaoSushant Rao

Thanks for the post vRay, but I don't think this solves my problem.

 

actionSupport is to hook into an elements event handling, I want to trigger the event of an element from Javascript, like btnCopy.click();

 

Thanks again !

aballardaballard

Your code looks ok to me for invoking the input element's "click()" method.    But according to my Javascript reference, calling the click method simulates a mouse click event but DOES NOT invoke any onclick handler associated with the input element. 

Sushant RaoSushant Rao

Thanks ! So how would I go about invoking an event handler for an element.

aballardaballard

if you just want to call the event handler, you can use   obect.onclick() to call whatever function is assigned to the onclick property.  (You may need to test it is non-null first). 

 

 

Sushant RaoSushant Rao

Just tried it, no luck!

 

                        <apex:inputText value="{!Params.ContractName}"     onclick="document.getElementById('{!$Component.zz}').onClick();"  />                  
                        <apex:inputText value="{!contractName}"  id="zz" onclick="alert('hi');" />

 

Thanks!

aballardaballard

Just tried it.  Turns out the DOM element name is "onclick" (all lowercase)  not "onClick".   It works if you change to

 

<apex:inputText value="{!Params.ContractName}"     onclick="document.getElementById('{!$Component.zz}').onclick();"  />                  

<apex:inputText value="{!contractName}"  id="zz" onclick="alert('hi');" />

This was selected as the best answer
Sushant RaoSushant Rao

Thanks a bunch, worked like a charm!