One way is by using document.getElementByID, as below -

<body>
  <h1>Adding 'a' and 'b'</h1>

  a: <input type="number" name="a" id="a"><br> b: <input type="number" name="b" id="b"><br>
  <button onclick="add(document.getElementById('a').value,document.getElementById('b').value)">Add</button>

  <script>
    function add(a, b) {
      var sum = parseInt(a, 10) + parseInt(b, 10);
      alert(sum);
    }
  </script>
</body>

Answer from Ankit on Stack Overflow
🌐
Talkerscode
talkerscode.com › howto › how-to-pass-parameter-in-javascript-function-from-html.php
How To Pass Parameter In JavaScript Function From HTML
OnClick() should be accompanied by a JavaScript function event element. As far as its parameters are concerned, that is the most important part. By using the Html button onClick function, we pass one parameter into the JavaScript function.
Discussions

How do I give parameters to a function in a javascript file that accesses HTML and CSS attributes? - Stack Overflow
I am trying to pass html parameters to a function in a javascript file, I tried it like this: More on stackoverflow.com
🌐 stackoverflow.com
How to call a javascript function(with parameters) in a HTML file?
If you want JavaScript to run when your html loads, all you need to do is put it in a script tag. This will call your function as soon as the html parses the tag. More on reddit.com
🌐 r/learnjavascript
2
1
January 30, 2023
how to send the parameter to the javascript function in HTML? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
How to pass parameters to a function through html and access by javascript? - Stack Overflow
Below is my HTML code to switch between the two tabs and display the content accordingly More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnjavascript › passing html form arguments to a js function
Passing html form arguments to a JS function : r/learnjavascript
May 26, 2022 - <form id="theForm" action="javascript:postData(theForm.username.value, theForm.password.value)"
🌐
Wlearnsmart
wlearnsmart.com › home › how to pass parameter in javascript function from html
How to Pass Parameter in JavaScript Function From Html
July 16, 2020 - Similarly, if a function defined multiple arguments pass like we define a single parameter to the above section. So, we can add multiple parameters with the help of a comma. <!DOCTYPE html> <html> <head> <title>multiple parameters function ...
🌐
GitHub
gist.github.com › evdokimovm › 8bad6e88b73769edcbc15594bbfe209d
Passing HTML input value as a JavaScript Function Parameter · GitHub
Click <script type="text/javascript"> function myfunctionName( name,age ){ document.getElementById("result").innerHTML += name; document.getElementById("ageshow").innerHTML += age; } </script> more learn about main points with the example of [How to Pass Parameter in JavaScript Function From Html]...
🌐
EyeHunts
tutorial.eyehunts.com › home › how to pass parameter in javascript function from html | example code
How to pass parameter in JavaScript function from HTML | Example code
June 6, 2022 - 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.
🌐
SheCodes
shecodes.io › athena › 6412-passing-parameters-to-javascript-functions
[JavaScript] - Passing Parameters to JavaScript Functions - | SheCodes
Discover how to pass parameters to JavaScript functions when you call them and understand how to use the value of the passed parameter.
Find elsewhere
🌐
W3Schools
w3schools.com › js › js_function_parameters.asp
JavaScript Function Parameters
In JavaScript, function parameters and arguments are distinct concepts: Parameters are the names listed in the function definition. Arguments are the real values passed to, and received by the function.
🌐
Reddit
reddit.com › r/learnjavascript › how to call a javascript function(with parameters) in a html file?
r/learnjavascript on Reddit: How to call a javascript function(with parameters) in a HTML file?
January 30, 2023 -

Hello i've been wondering is there a way to call a javascript function from a HTML file and pass it parameters in that call? I found the onclick option but i need the function to load automatically and onload doesn't work.

Example: I would like to call myFunction(30,60) from HTML file.

🌐
EyeHunts
tutorial.eyehunts.com › home › pass parameter to javascript function onclick in html | example code
Pass parameter to JavaScript function onclick in HTML | Example code
May 16, 2021 - It’s very easy to Pass parameter to JavaScript function using the onClick() function. If sending a string value then use double” ” or single ” quote in the function. You just need to add some quotes around the text.
🌐
Stack Overflow
stackoverflow.com › questions › 22183718 › how-to-pass-parameters-to-a-function-through-html-and-access-by-javascript
How to pass parameters to a function through html and access by javascript? - Stack Overflow
Also, you must switch from "onLoad" to "No wrap - in <head>" on the left if you want the functions to be available at the window scope. Changing these things results in a working example. 2014-03-04T22:43:55.483Z+00:00 ... But this doesn't work on my local machine. What changes should i make? like on jsfiddle, "No wrap-in <head>" was needed instead of "on load" 2014-03-04T22:56:26.23Z+00:00 ... Will P. Over a year ago · the <head> setting on fiddle basically puts the javascript code inside <script> tags in the head of the document, instead of executing the code inside the onLoad method of the page.
Top answer
1 of 2
3

For the first part of your question, you can do this by using event method for calling your function.

Example:

function myFunction() {
    document.getElementById("demo").innerHTML = "Hello World";
}
<p>Click the button to trigger a function that will output "Hello World" in a p element with id="demo".</p>

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

Or use javascript event listener.

Exemple:

document.getElementById("myBtn").addEventListener("click", displayDate);

function displayDate() {
    document.getElementById("demo").innerHTML = Date();
}
<p>This example uses the addEventListener() method to attach a click event to a button.</p>

<button id="myBtn">Try it</button>

<p id="demo"></p>

Or use jquery event listener.

Example:

$(document).ready(function(){
    $("p").click(function(){
        alert("The paragraph was clicked.");
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Click on this paragraph.</p>

For the second part of your question you can send parameters by passing them when you call your function, or getting them when you are using your function.

Example for sending parameters when calling the function:

function changeText(id) {
    id.innerHTML = "Ooops!";
}
<h1 onclick="changeText(this)">Click on this text!</h1>

Example for getting when you are in the function (JS):

function myFunction() {
    var x = document.getElementById("myText").value;
    document.getElementById("demo").innerHTML = x;
}
First Name: <input type="text" id="myText" value="Mickey">

<p>Click the button to display the value of the value attribute of the text field.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

Example for getting when you are in the function (Jquery):

$(document).ready(function(){
    $("button").click(function(){
        var first_name = $("#user_f").val();
        var last_name = $("#user_l").val();
        var name = first_name + " " + last_name;
        $("#user").val(name);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p> First Name: <input type="text" id="user_f"></p>

<p> Last Name: <input type="text" id="user_l"></p>

<p>Name: <input type="text" id="user"></p>

<button>Set the value of the input field</button>

If you want a server-side variable you can use this form. In your function js use this var x = <?php echo $valor_x; ?>; . This works when you have your PHP in the same page that you have your JS.

2 of 2
0

It looks like you have many elements (i.e. one per row) with ID's like total\[\]. Appart from not being particularely attractive IDs (I would personally not put symbols in IDs) they are not unique, which IDs must be! I would probably rename them to something like total-<?php echo $i; ?> if that makes sense.

Finally, your copy_text function is being defined multiple times (once for each PHP loop, check the output from PHP), which is also causing things to go wrong. I would make it a one-parameter function, i.e. copy_text(id) and only define it once outside the loop, but if you think that is complicated you might just keep it the way it is, but name each function copy_text_<?php echo $i; ?>.

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-pass-string-parameter-in-onclick-function
JavaScript - Pass String Parameter using onClick function - GeeksforGeeks
July 11, 2025 - When a user clicks an element, ... value—along with it. This can be easily achieved by passing a string parameter inside the onClick attribute of an HTML element....
🌐
Coderanch
coderanch.com › t › 46224 › frameworks › pass-parameters-javascript-function-click
How to pass parameters to javascript function on click of html:link (Struts forum at Coderanch)
this forum made possible by our volunteer staff, including ... ... I am displaying links using logic:iterate tag. Onclick of link I need to window.open with some paramters passed to the javascript openNewWin("FormId", "BranchId") which implements window.open() functionality.
🌐
Medium
medium.com › wineofbits › passing-arguments-to-embedded-javascript-f9932287f7f7
Passing arguments to embedded Javascript | by Dhanraj Acharya | wineofbits | Medium
June 28, 2018 - So, you’re writing an embedded script and you want to pass some arbitrary arguments and parameters to your javascript. How do you do it? This quick little tutorial will show you an example. This is the easiest method. HTML5 now supports arbitrary parameters, so you can pass any arguments to your function with that.
🌐
Stack Overflow
stackoverflow.com › questions › 13268922 › passing-parameters-to-a-js-function-from-html-code
javascript - Passing parameters to a JS function from HTML code - Stack Overflow
November 7, 2012 - <select id="p1_choise" onchange="change(this,document.getElementById('image'))"> <option value=0>Choose</option> <option value=1>Rock</option> <option value=2>Paper</option> <option value=3>Scissors</option> </select> <p><img src="rps.jpg" id="image"></p> <script> function change(options,imageElement) { var icon = options.value; switch (parseInt(icon)) { case 1: imageElement.src="rock.jpg"; break; case 2: imageElement.src="paper.jpg"; break; case 3: imageElement.src="scissors.jpg"; break; } } </script> ... Updated so you can specify the image and the select elements as parameters 2012-11-07T11:47:28.537Z+00:00