I know I can do this as an object but am wondering if there is some way to do this that I haven't thought of.
This is what I want to do, but it doesn't work the way I want of course:
function test1(vari) {
vari = 2;
}
var myvar1 = 1;
test1(myvar1);
console.log(myvar1);
This works, but I don't really want to make all the variables objects:
function test2(vari) {
vari.val = 2;
}
var myvar2 = { val: 1 };
test2(myvar2);
console.log(myvar2.val);
This also works, but not an acceptable solution:
function test3(vari) {
eval(vari + ' = 2;');
}
var myvar3 = 1;
test3('myvar3');
console.log(myvar3);
Is there a better option than these?
Updated
A better example of what I would like to do:
function test5(vari, onblur) {
document.querySelector('body').innerHTML += ``;
el = document.getElementById(`ctl_${vari}`);
el.addEventListener('blur', function(e) {
vari = e.target.value; //Doesn't update original variable
onblur(e);
});
return el;
}
var myvar5 = 'test';
var ele = test5(
myvar5,
function(e) {
//Could set myvar5 = e.target.value but not wanting to do this for every single field
console.log(myvar5);
}
);
0 comments:
Post a Comment
Thanks