Search code examples
javascriptfunctionvariable-names

variable in variable name for javascript


The command "alert(numOne);" shows f_name. Thus, I want the the last line of the function to become document.f_name.submit(); but my current idea doesn't work. Is there a way to make this happen?

    function submitform(e)
{
    var numOne = e.id;
    alert(numOne);
    if (        confirm("Proceed?")){
          document.numOne.submit();
        }

}

Solution

  • You can use bracket notation for this purpose,

    document[numOne].submit();
    

    And your full code would be,

    function submitform(e) {
      if (confirm("Proceed?")) {
        document[e.id].submit();
      }
    }