VUEJS: passing value of javascript button - javascript

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);
}

Related

How to Add Diff Links to JS Buttons?

I created multiple buttons with Vanilla JS and want them to link to a different page. I'm thinking of mapping over the 'links' array but need a little refresher on how to target each individual button leading to the right page?
const multipleButtons = () => {
// let i = 0;
let body = document.getElementsByTagName("body")[0];
var optionText = ["Fit Guide", "Care", "Materials"];
var links = ['fitGuide.html', 'care.html', 'materials.html'];
for (let i = 0; i < optionText.length; i++) {
let button = document.createElement("button");
// button.innerHTML = 'Button '+i;
button.innerHTML = optionText[i];
body.appendChild(button);
button.addEventListener("click", function() {
// alert(this.innerHTML);
});
}
}
multipleButtons();
Buttons work just fine, just gotta add the appropriate links. I know this can easily be done in HTML/CSS but I'm supposed to do this in plain JS.
You can change your event listener to
button.addEventListener("click", function() { window.location.href = links[i]; });
If you have to use buttons and not an a tag, something like this:
const multipleButtons = () => {
// let i = 0;
let body = document.getElementsByTagName("body")[0];
var optionText = ["Fit Guide", "Care", "Materials"];
var links = ['fitGuide.html', 'care.html', 'materials.html'];
for (let i=0; i < optionText.length; i++) {
let button = document.createElement("button");
// button.innerHTML = 'Button '+i;
button.innerHTML = optionText[i];
body.appendChild(button);
button.addEventListener ("click", function() {
window.location.href = "http://www.baseurl.com/path/to/page/"+links[i];
});
}
}
multipleButtons();
EDIT: changed to href instead of replace
for loops have been proven to be faster, but forEach loops are a bit more readable so I suggest you use those. It's also a good idea to use const for variables that won't change.
One last thing is that it's better to use <a> tags because that's what they were designed to do - like Quentin mentioned in the comments. You can style them to look like buttons if you want.
const multipleButtons = () => {
const optionText = ["Fit Guide", "Care", "Materials"];
const links = ['fitGuide.html', 'care.html', 'materials.html'];
optionText.forEach(function(text, index) {
const anchor = document.createElement("a");
anchor.innerText = text;
anchor.href = links[index];
document.body.appendChild(anchor);
anchor.addEventListener("click", function() {
alert(this.innerText);
window.location.href = this.href;
});
});
}
multipleButtons();
a {
margin-right: .5em;
}

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

How to have a static onclick parameter in HTML button?

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);
}

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