JavaScript | Pass string parameter in onClick function
The task is to pass a string as a parameter on onClick function using javascript, we’re going to discuss few techniques.
Example 1: This example simply put the argument which is string in the onClick attribute of the button which calls a function with a string as an argument using onClick() method.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Pass string parameter in onClick function. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 19px; font-weight: bold;"> </p> <button onclick="GFG_Fun('Parameter'); "> click here </button> <p id="GFG_DOWN" style="color: green; font-size: 24px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); up.innerHTML = 'Click on button to pass the '+ 'string parameter to the function'; function GFG_Fun(parameter) { down.innerHTML = "String '" + parameter + "' Received"; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: This example simply put the argument which is a string in the onClick attribute of the button which calls a function with string as an argument using onClick() method. Here the input button is created dynamically. This example uses the same approach as the previous one.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Pass string parameter in onClick function. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 19px; font-weight: bold;"> </p> <p id="GFG_DOWN" style="color: green; font-size: 24px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); var inputEl = document.createElement('input'); inputEl.type = 'button'; inputEl.value = "Click here"; inputEl.addEventListener('click', function() { GFG_Fun('Parameter'); }); document.body.insertBefore(inputEl, down); up.innerHTML = 'Click on button to pass the '+ 'string parameter to the function'; function GFG_Fun(parameter) { down.innerHTML = "String '" + parameter + "' Received"; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- Passing a function as a parameter in JavaScript
- How to set a default parameter value for a JavaScript function ?
- How to pass a PHP array to a JavaScript function?
- How to get the javascript function parameter names/values dynamically?
- Call multiple JavaScript functions in onclick event
- How to stop event propagation with inline onclick attribute in JavaScript ?
- How to add a parameter to the URL in JavaScript ?
- JavaScript | Rest parameter
- How to pass JavaScript variables to PHP ?
- How to pass variables and data from PHP to JavaScript ?
- JavaScript | String() Function
- How to create a function from a string in JavaScript ?
- How to call function from it name stored in a string using JavaScript?
- HTML | DOM onclick Event
- HTML | onclick Event Attribute
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



