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 OverflowOne 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>
Firstly an elements ID should always be unique. If your element IDs aren't unique then you would always get conflicting results. Imagine in your case using two different elements with the same ID.
<form>
a: <input type="number" name="a" id="a"><br>
b: <input type="number" name="b" id="b"><br>
<button onclick="add()">Add</button>
</form>
<script>
function add() {
var a = document.getElementById('a').value;
var b = document.getElementById('b').value;
var sum = parseInt(a) + parseInt(b);
alert(sum);
}
</script>
How do I give parameters to a function in a javascript file that accesses HTML and CSS attributes? - Stack Overflow
How to call a javascript function(with parameters) in a HTML file?
how to send the parameter to the javascript function in HTML? - Stack Overflow
How to pass parameters to a function through html and access by javascript? - Stack Overflow
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.
Your example almost works - the problem is when you execute this line:
var mypara = document.getElementById("para1");
The element you're refering to does not yet exist. You could fix it by just going inline:
<h1 onclick="changetext(document.getElementById('para1'))">...</h1>
Live example for this approach: http://jsfiddle.net/Gw5CG/2/
or perhaps just pass the id to the method:
<h1 onclick="changetext('para1')">...</h1>
and change the method to do the getElementById:
function changetext(mypara)
{
document.getElementById(mypara).innerHTML="Ooops!";
}
Live example for this approach: http://jsfiddle.net/Gw5CG/1/
The element doesn't exist yet when you're trying to get it.
Why not just get it in the event handler
<!DOCTYPE html>
<html>
<head>
<script>
function changetext() {
document.getElementById("para1").innerHTML = "Ooops!";
}
</script>
</head>
<body>
<h1 onclick="changetext()">Click this text to change the content of following paragraph</h1>
<p id="para1">this is a paragraph I would like to change</p>
</body>
</html>
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.
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; ?>.
If you are passing a string, then use quotes around the string. As single and double quotes are equivalent, it would make most sense to use single quotes in your example, otherwise you end up having to escape quotes and it starts getting messy.
If you are passing a literal number or the name of a variable, do not use quotes.
More information on quotes: When to Use Double or Single Quotes in JavaScript.
Fixed it, after stepping away for an hour.
I either need to re-declare the 1st "x" in the function or change the "x" to another variable. I have changed the variable to "xVar" and it is working.
Thanks anyway.
function showSelectsVar (x) {
xVal = document.getElementById(x).value;
var insertTextVar = document.createTextNode(xVal);
var child = document.getElementById(x);
child.parentNode.insertBefore(insertTextVar,child);
}