Hello, I was wondering if there would be a way I could generate elements dynamically using something like this, where qtyElements is an array of all the elements that start with qty.

var qtyElements = document.getElementsByName('qty*');
for(var i = 1; i < qtyElements.length + 1; i++)
   // var name is cqty + whatever number at i is
   var cqty + i = qtyElements[i - 1];
8 Years
Discussion Span
Last Post by ~s.o.s~

well for what I'm doing I don't want take time to learn your jdomp you are promoting.


Maybe something like this snippet will help you getting started. Ensure that the elements you are trying to access are form fields enclosed in the form tag.

    <meta http-equiv="Script-Content-Type" content="text/javascript">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
     * A placeholder function which will be called on the button click which
     * in turn invokes the function which contains the core logic.
     * @param frm The reference to the form element
     * @param pattern THe pattern to be used for matching.
    function calculateForPattern(frm, pattern) {
      var sum = calculate(frm, pattern);
      alert("The sum of all elements whose names start with *" +
        pattern + "* is " + sum);      
     * Calculate the sum of values of the text fields on the 
     * document whose name start with "qty"
     * @param frm The reference to the form element
     * @param pattern THe pattern to be used for matching.
     * @returns The sum of the values of all the form fields
     *  whose name match the given pattern.
    function calculate(frm, pattern) {
      // Duck out if no proper form reference / pattern supplied.
      if(!frm || !pattern) {
      // Assumption: Only text fields have names starting with the given
      // pattern. For other form fields (drop downs, radio buttons) add
      // the required handling.
      var elems = frm.elements, sum = 0;
      for(var i = 0, maxI = elems.length; i < maxI; ++i) {
        var val = elems[i].value ? elems[i].value : null;        
        if(val) {
          // Do away with inputs of the form 3E4 which are valid JS
          // numbers. Allow only digits and dots and if proper input
          // is supplied, try to parse the number out of the value.
          var num = /^[0-9.]+$/.test(val) && Number(val);
          // Don't perform the addition if num evaluates to NaN.
          sum = sum + (num ? num : 0);
  <form id="frm" name="frm" action="#">
    <div id="qtyElems">
      <span>First: </span><input id="qtyOne" name="qtyOne" value="5">
      <span>Second: </span><input id="qtyTwo" name="qtyTwo" value="5">
      <input type="button" 
        onclick="calculateForPattern(this.form, 'qty');" value="Calculate!">

so would a regex work to get the pattern out so we get the ones that start with qty?


Certainly, a regular expression can be very well used for filtering out elements based on the pattern which can then be used to do additional processing.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.