简体   繁体   中英

How can I make two functions a single function in Javascript?

I have these two functions:

function myFunction() {
    var x = document.getElementById("myTextarea").value;
    document.getElementById("demo").innerHTML = x;
}

function myFunction1() {
    var x = document.getElementById("myTextarea1").value;
    document.getElementById("demo1").innerHTML = x;
}

and I do not succeed to merge them into a single function.

function myFunction(inputId,targetId) {
        var x = document.getElementById(inputId).value;
        document.getElementById(targetId).innerHTML = x;
}
myFunction('myTextarea','demo');
myFunction('myTextarea1','demo1');

Do you mean something like:

function myFunction() {
    var textArea = document.getElementById("myTextarea").value;
    var textArea1 = document.getElementById("myTextarea1").value;
    document.getElementById("demo").innerHTML = textArea;
    document.getElementById("demo1").innerHTML = textArea1;
}

insertValue('Value','Inner') this is a function which get 2 arguments, first - value id, and second inner id. You can put any ID and use this multipurpose function

 function insertValue(id_value, id_inner) { let x = document.getElementById(id_value).value; document.getElementById(id_inner).innerHTML = x; } insertValue('Value','Inner');
 <input id="Value" type="" name="" value="value which you want to put"> <div id="Inner">inner text which you will dont see</div>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM