+ Start a Discussion
PrasadVRPrasadVR 

How to call java script function when i click on command button?

Hi All ,

 

         I am not awre how to use java script in visualforce , I have a requriment if the user provides some value in input text box and if he click on command button then i need an aleret , i am awre that how to call to java script function when user click on command button, but i am not awre how add condition in java script function if(opp!=null) then alert('WC');

 

however it's working fine.

<script>

  function check() {

  alert('Welccome ');

}

</script>

<apex:commandbutton value="DO" action={!submit} onclick="return check();"/>

 

If change my code to this. it's not working.

 

<script>

  function check() {

 if(opp!=null){

  alert('Welccome ');

} }

</script>

<apex:commandbutton value="DO" action={!submit} onclick="return check();"/>

<apex:inputtext value="{!opp}"/>

 

and one more thing. i have outputlink when i click on that it will redirects to new page.however if my selectlist value is null menas i don't want to call this method and i need a pop up says please select existing record. 

 

 

 

robdobbyrobdobby

Hello,

 

Keep in mind the difference between the server and the browser.  When you have a bound variable like {!opp}, it is going to have the value it had on the server.  So it  started as null on the server, the value null is sent to the web browser both in your 

 

<apex:inputtext value="{!opp}" id="oppTextInput"/>

 

and in the reference in the JavaScript.  The user types in a value in the text field, but your JavaScript still sees null.  You can have your JavaScript check the HTML tag.  The VF tag looks something like this in the web browser

 

<input id="oppTextInput" type="text" name="oppTextInput" />

 

You could have your JavaScript check the HTML input tag:

 

<script>

  function check() {

    if(document.getElementById('oppTextInput').value!=null){

     alert('Welcome ');

    }

  }

</script>

 

 

 

PrasadVRPrasadVR

Hi ,

 

       it's working every time i want only if the user enters value and  i want use input text here because i need to pass this value to controller. i am not sure how to pass html input value into controller

Ranu JainRanu Jain
Prad47Prad47

We can use jQuery in this case.We can check the value enter by user in following way on command button  - 

 

<apex:commandbutton value="DO" action={!submit} onclick="if ($('input[Id$=ComponentId]').val() != '') check();"/>

 

where ComponentId is id of input text box.

 

Let me know if it works.

Tø

Hi vrpnaidu,

 

the problem is that 'opp' no is nothing on the java script and the web.

 

Change  <apex:inputtext value="{!opp}" /> for  <apex:inputtext  id="opp" value="{!opp}" />

And in the scrip you need createa a var 

function check() {

 if(opp!=null){

  alert('Welccome ');

} }

 


to

 

function check(){

 var opp = document.getElementById("opp");

 if(opp!=null){

   alert('Welcome');

 } 

}