How to have a static onclick parameter in HTML button? - javascript

How can I dynamically create HTML buttons, in Javascript, that when users click on them they call a same function, but with a different parameters.
For example:
function a(param){console.log(param);};
for (int i = 0; i < 10; i++)
{
button = document.createElement('button');
button.onclick = <place a function that when button is clicked, calls function a with parameter i>;
}
Can someone give me a clue?

Call the a function:
button.setAttribute('data-param', i);
button.onclick = function () {a(this.getAttribute('data-param'));};
jsFiddle: http://jsfiddle.net/systemovich/asw3myqg/1/

function b(param){
var myParam = param;
return function a() {
console.log(myParam);
}
}
for (int i = 0; i < 10; i++)
{
button = document.createElement('button');
button.onclick = b(i);
}

Related

VUEJS: passing value of javascript button

I am trying to pass the value of a button to a function when it is clicked. Because the buttons were created as a javascript element I'm not sure how to do it.
methods:{
createButtons() {
var i;
var rows =["9","8","7","6","5","4","3","2","1","0","•","="];
var elDiv = document.getElementById("myDIV");
for (i=0; i<12; i++){
var btn = document.createElement("BUTTON");
btn.value = i
btn.style.height = "40px"
btn.textContent = rows[i];
btn.onclick = buttonvalue;
elDiv.appendChild(btn);
}
var pressedbutton = document.getElementById("calculate");
pressedbutton.remove();
},
}
}
function buttonvalue(i){
alert(i);
}
This is an XY problem. Don't create DOM elements manually like this, that's what Vue is for.
But to answer your question, you can do something like this:
const captureI = i;
btn.onclick = () => buttonvalue(captureI);
I copied i into a new local variable because i changes value by the for loop.
Or you can just write the for loop like this instead:
for (let i = 0; i < 12; i++) {
// ... omitted code ...
btn.onclick = () => buttonvalue(i);
}

How to access dynamically created button?

btnUpdate = document.createElement("BUTTON");
var t = document.createTextNode("Update");
btnUpdate.id = 'update0';
btnUpdate.appendChild(t);
tabCell.appendChild(btnUpdate);
I have a simple line of code where I have created a button with my javascript. How do I access this button through the same javascript file? I want to add onClick feature to it.
document.getElementById("update0").onclick = edit_row(0);
I tried doing so by adding the above line of code, but now it won't display the table but straight away jumps to the edit_row() function.
Edit:
function showCustomer() {
var obj, dbParam, xmlhttp, myObj, x, txt = "",tabCell;
var btnUpdate;
obj = { "table":"Successful", "limit":20 };
dbParam = JSON.stringify(obj);
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myObj = JSON.parse(xmlhttp.responseText);
console.log(myObj);
var col = [];
for (var i = 0; i < myObj.length; i++) {
for (var key in myObj[i]) {
if (col.indexOf(key) === -1) {
col.push(key);
}
}
}
key="Update";
col.push(key);
console.log(col);
// CREATE DYNAMIC TABLE.
var table = document.createElement("table");
// CREATE HTML TABLE HEADER ROW USING THE EXTRACTED HEADERS ABOVE.
var tr = table.insertRow(-1); // TABLE ROW.
for (var i = 0; i < col.length; i++) {
var th = document.createElement("th"); // TABLE HEADER.
th.innerHTML = col[i];
tr.appendChild(th);
}
// ADD JSON DATA TO THE TABLE AS ROWS.
for (var i = 0; i < myObj.length; i++) {
tr = table.insertRow(-1);
tabCell = null;
for (var j = 0; j < col.length-1; j++) {
tabCell = tr.insertCell(-1);
tabCell.innerHTML = myObj[i][col[j]];
}
tabCell = tr.insertCell(-1);
btnUpdate = document.createElement("BUTTON");
var t = document.createTextNode("Update");
btnUpdate.id = 'update'+i;
btnUpdate.appendChild(t);
tabCell.appendChild(btnUpdate);
}
tr = table.insertRow(-1);
tabCell = null;
for (var j = 0; j < col.length-1; j++) {
tabCell = tr.insertCell(-1);
tabCell.innerHTML = " ";
}
tabCell = tr.insertCell(-1);
var btn = document.createElement("BUTTON");
var t = document.createTextNode("Add Row");
btn.appendChild(t);
tabCell.appendChild(btn);
document.getElementById("update0").addEventListener = function (){
edit_row(0);
};
// FINALLY ADD THE NEWLY CREATED TABLE WITH JSON DATA TO A CONTAINER.
var divContainer = document.getElementById("showData");
divContainer.innerHTML = "";
divContainer.appendChild(table);
}
};
xmlhttp.open("POST", "http://localhost:8090/Vaccine", true);
xmlhttp.setRequestHeader("Content-type", "application/JSON");
xmlhttp.send("x=" + dbParam);
}
function edit_row(no)
{
alert("HELLO");
}
With this line :
document.getElementById("update0").onclick = edit_row(0);
You are not "attaching" the click event to the edit_row function. You're setting the onclick property with the result of the edit_row(0) invokation.
Also, don't use the onclick property.
Use the addEventListener function instead.
document.getElementById("update0").addEventListener("click", function () {
edit_row(0);
});
If you need a reason : by overwriting the onclick property, you could be disabling any other click event listener on your elements. By using addEventListener(), you can have several events listener on the same element/event couple.
And you can do this right after you created the button. You don't need to get it by its id later.
Your code would look like this :
btnUpdate = document.createElement("BUTTON");
var t = document.createTextNode("Update");
btnUpdate.id = 'update0';
btnUpdate.appendChild(t);
btnUpdate.addEventListener("click", function () {
edit_row(0);
});
You have to do that in callback of on click event. If you inline, it executes directly when javascript reading your code.
document.getElementById("update0").onclick = function (){
edit_row(0);
};
How do I access this button through the same javascript file?
The same way you've been accessing it all along.
It is stored in the btnUpdate variable. Use that.
but now it won't display the table but straight away jumps to the edit_row() function.
That is because you are calling edit_row and setting its return value as the click handler.
Since you want to pass arguments to it, the easiest thing to do is to create a new function.
function edit_row_first_argument_0 () {
edit_row(0);
}
button.addEventListener("click", edit_row_first_argument_0);
(You can use an anonymous function expression, I use the verbose approach above for clarity).
Try this:
btnUpdate = document.createElement("BUTTON");
var t = document.createTextNode("Update");
btnUpdate.id = 'update0';
btnUpdate.appendChild(t);
tabCell.appendChild(btnUpdate);
btnUpdate.addEventListener("click", (e) => {
// this linked to btnUpdate
// Here make whatever you want
// You can call edit_row now
edit_row(0)
})
It seems that your button is not in the DOM yet, so you are not able to find it with document. You can use the variable btnUpdate if it is in the same file like btnUpdate.onclick = function() {}, or using addEventListenerlike btnUpdate.addEventListener('click', function() {}).
Also, it seems you are executing the edit_row(0) function. You need to put it inside a function like
btnUpdate.addEventListener('click', function() {
edit_row(0);
})
You call the function when you have () at the end so
document.getElementById("update0").onclick = edit_row(0);
will immediately call edit_row
Why not do this instead:
btnUpdate = document.createElement("BUTTON");
var t = document.createTextNode("Update");
btnUpdate.id = 'update0';
btnUpdate.onclick=function() {
edit_row(this.id.replace("update","")); // or use a data-attribute
}
btnUpdate.appendChild(t);
tabCell.appendChild(btnUpdate);
or use event delegation:
Native JS equivalent to jquery delegation

A javascript function that complete another function sent as a parameter?

In the following javascript function window.setInterval(function,milliseconds) I can do the following:
var myvarible = function(){alert('Popup');};
window.setInterval(myvarible,5000);
now every 5 seconds my webpage will alert the message 'popup'
How can I make my own function do the parameter myvarible, as see above? This is what I am trying below, but it does not work.
text = "";
var myvarible = function() {
for (i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
}
}
function howLong(the_function){
var starting_time = new Date().getMilliseconds();
the_function //do the function here
var finishing_time = new Date().getMilliseconds();
var difference = finishing_time-starting_time;
document.write(difference);
}
howLong(myVarible); //call my howLong function
I hope you understand me, I have tried to make myself as clear as possible and I am new to javascript so please try to keep the answers as simple as possible.
Thanks for your help
Put () after the variable to call it as a function:
the_function(); // do the function here
You have to make 3 corrections.
1.) calling howLong(myVarible); should be howLong(myvarible); because you are defining function as myvarible
2.)You have to call the function add () in function howLong
`the_function() //do the function here`
3.) define cars.
This worked for me
<script>
text = "";
var cars = [1,2,3];
var myVarible = function() {
for (i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
}
}
function howLong(the_function){
var starting_time = new Date().getMilliseconds();
the_function() //do the function here
var finishing_time = new Date().getMilliseconds();
var difference = finishing_time-starting_time;
document.write(difference);
}
howLong(myVarible); //call my howLong function
</script>

Dynamically create buttons with different event calls

I have a problem in the following JavaScript function.
I am trying to create buttons dynamically based on details from the results variable.
The button are created and an event is attached but it seems each button has the exact same event attached.
I need the address variable to be different for each event attached to a button and for that button then to be added to replace text in my macField variable.
function (results) {
var r;
var x, i;
var btn;
for (i = 0; i < results.length; i++) {
app.display("Paired to" + results[i].name + results[i].address);
x = document.getElementById("message2");
r = results[i].address;
w = document.getElementById('macField').value;
btn = document.createElement('input');
btn.onclick = function () {
document.getElementById('macField').value = r;
};
btn.setAttribute('type', 'button');
btn.setAttribute('name', 'sal' + [i]);
btn.setAttribute('id', 'Button' + [i]);
btn.setAttribute('value', results[i].name);
appendChild(btn);
}
}
function (error) {
app.display(JSON.stringify(error));
}
Use immediate function. Change this part:
btn.onclick = function () {
document.getElementById('macField').value = r;
};
like this:
(function(r){
btn.onclick = function () {
document.getElementById('macField').value = r;
};
})(r);
Have a look: http://jsfiddle.net/uebD8/1/
create your function seperately like below
function macFieldValue(val)
{
document.getElementById('macField').value = val;
}
and set button onclick attribute like this
btn.setAttribute('onclick', 'macFieldValue('+r+')');
Just modifying this part:
btn = document.createElement('input');
btn.onclick = function () {
document.getElementById('macField').value = r;
};
to
btn = document.createElement('input');
btn.setAttribute('some_id', r);
btn.addEventListener('click',
function (e) {
var addr = e.source.some_id;
document.getElementById('macField').value = addr;
}, false);
You are hitting an unfortunate curse of JS. Your code would have worked if it wasn't within a for-loop. In any case, you should be using addEventListener.

attach function with parameter from javascript

I have this function:
function addButtonLookup() {
var element = document.getElementById("btnToolBar");
var index;
for (var i = 0; i < lookupArray.length; i++) {
index = i;
var btn = document.createElement('input');
btn.type = 'button';
btn.value = '' + lookupArray[i];
btn.name = 'btnLookup' + i;
btn.id = i;
btn.className = 'CommonButtonStyle';
element.appendChild(btn);
btn.onclick = function() {
debugger;
tblExcpression.WriteMathElement(lookupArray[i], lookupArray[i]);
};
}
}
onbutton click the i is undefined
Instead of this:
btn.onclick = function() {
debugger;
tblExcpression.WriteMathElement(lookupArray[i], lookupArray[i]);
};
Try this:
btn.onclick = (function(i) {
return function() {
debugger;
tblExcpression.WriteMathElement(lookupArray[i], lookupArray[i]);
}
})(i);
The issue with the first version is that the i variable is copied from the current scope. However the i variable varies in the current scope (it's part of a for loop), this is why you're getting this weird behavior.
By passing the i variable as a paremeter to a new function (like the second example) the current i variable is copied.
You should take a look at how Closures work in JavaScript.

Categories