Search code examples
javascriptfunctionparametersinvoke

can i add parameters in a child function in javascript?


function smth(txt, a, b, c) {
  document.getElementById(txt).innerHTML = a + b + c;
 }


function whatever() {
  function smth(txt1, "hello ", "my ", "friend");
  function smth(txt2, "how ", "are ", "you");
  function smth(txt3, "i am ", "good ", "thankyou");
 } 

is this a thing, (it must be) and how do i get the result if my code is wrong, also sorry about the format of the question i dont know how to ask it.

<button type="button" onclick="whatever()">save</button>

<p id="txt1"></p>
<p id="txt2"></p>
<p id="txt3"></p>

Solution

  • function smth(txt, a, b, c) {
    document.getElementById(txt).innerHTML = a + b + c;
    }
    
    
    function whatever() {
     smth("txt1", "hello ", "my ", "friend");
     smth("txt2", "how ", "are ", "you");
     smth("txt3", "i am ", "good ", "thankyou");
    } 
    

    i had to remove the word "function" from before the smth, and surround the "txt"s with quotations.