Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a function from a button click in JSP

I have the following function:

<script>
function assign()
{
String val="";
String val = document.form1.text1.value;
System.out.println(val);
}
</script>

i am trying to call the function in the button click as shown below:

  <button type="button" onclick="assign()">Display</button> 

When i click on the button nothing happens. I want the string value in the text box to be printed on the console.Please help

like image 587
Sindu_ Avatar asked Jan 13 '23 18:01

Sindu_


1 Answers

First of all, if you are trying to call JavaScript on button click, then your syntax is wrong. See, you are mixing Java with JavaScript (code) in script tag, that is invalid.

Use like this:

<script>
    function assign() {
        var val = "";
        val = document.form1.text1.value;
        alert(val); or console.log(val);
    }
</script>

and

 <button type="button" onclick="javascript:assign();">Display</button> 
like image 149
Parkash Kumar Avatar answered Jan 22 '23 13:01

Parkash Kumar