I have a dynamic button with looping and I have the onclick attribute function, like below :
for (var j=0; j<= 1; j++){
btndisplay= document.createElement("input");
btndisplay.setAttribute("type", "button");
btndisplay.setAttribute("style","height:80px;width:60px");
btndisplay.setAttribute("onclick", "myFunction"+ (j+1) +"(this.name)");
document.getElementById('divButtons'+(j+1)).appendChild(btndisplay);
}
How to simplify many functions as below, into a single function?
function myFunction1(name)
{
if (document.getElementById('value1').innerHTML==""){
document.getElementById('value1').innerHTML = name;
}
}
function myFunction2(name)
{
if (document.getElementById('value2').innerHTML==""){
document.getElementById('value2').innerHTML = name;
}
Use another parameter, one for the name
and another for the id
:
function myFunction(id, name)
{
if ( document.getElementById(id).innerHTML=="" )
document.getElementById(id).innerHTML = name;
}
you have created:
<input type="button" onclick="myFunction(this)">
<input...
...
the Function is:
function myFunction (that) {
if(that.innerHTML === "") {
that.innerHTML = that.name;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.