title: Passing JavaScript function parameters to jQuery
body: <p>I am working on some legacy code. For the first phase of refactoring, I'm updating all the scripts to jQuery, because they're inconsistently switching between vanilla JS and jQuery. Everything has been easy to convert. However, I'm having issues trying to figure out how to get parameters from a JavaScript function into a jQuery selector/function.</p>
  <p>For context, the original authors wrote this generic JS function:</p>
  <pre><code>    // Submits the specified form and command.
      function globalSubmitForm(formName, elementName, commandText) {
          document.getElementById(elementName).value = commandText;
          document.getElementById(formName).submit();
      }
  </code></pre>
  <p>I rewrote the function as:</p>
  <pre><code>    function globalSubmitForm(formName, elementName, commandText) {
          $('#elementName').attr('value', 'commandText');
          $('#formName').submit();
      }
  </code></pre>
  <p>However, as I'm sure you will already see, the jQuery selector and functions are reading the text verbatim, not referencing the function parameters.</p>
  <p>I haven't tried much experimenting. I'm still new to JavaScript and jQuery and have just been living inside the jQuery documentation for much of my refactoring. However, I have not been able to find any documentation or answers.</p>
  <p>The one thing I have tried is this:</p>
  <pre><code>    $('#' + elementName).attr('value', commandText);
      $('#' + formName).submit();
  </code></pre>
  <p>However, I am not at a point where I can test this quite yet. My IDE (PhpStorm 2021.3) no longer inspects that the JS function parameters are unused, but I feel as though there might be a better way to write this.</p>
  <p>I think that removing the quotes from commandText will work correctly as the expected argument is a string. But concatenating the parameters in the jQuery selector feels clunky and does not sit well with me.</p>