Skip to content

How to pass parameter in JavaScript function from HTML | Example code

  • by

Use the onclick attribute in a button tag with the function name and pass value in this function. With this method, you can also take input from users and pass parameters in the JavaScript function from HTML.

<button onclick="Namefunction('John')">Click</button>

function functionName( parameters ){
	/// pass any values 
}

Example pass parameter in JavaScript function from HTML

Simple example code to pass the parameter in JavaScript function from Html. Include a JavaScript function event element of onClick() in the HTML button tag.

<!DOCTYPE html>
<html>

<body>
  <button onclick="Namefunction('John')">Click</button>

  <script type="text/javascript">

    function Namefunction(name){

      alert(name);

    }

  </script>
</body>
</html>

Output:

How to pass parameter in JavaScript function from HTML

Pass Multiple Parameters to JavaScript Function onClick

Simply add multiple parameters with the help of a comma.

<button onclick="myfunctionName('John', 25)">Click</button>

  <script type="text/javascript">

    function myfunctionName( name, age ){
      alert(name+ " "+age)
    }

  </script>

Do comment if you have any doubts or suggestions on this Python Js function topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *