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];
}

Recommended Answers

All 5 Replies

See the JDOMP thread for a simple way using the DOM

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.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Script-Content-Type" content="text/javascript">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Example</title>
    <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) {
        return(0);
      }
      // 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);
        }
      }
      return(sum);
    }
    </script>
</head>
<body>
  <form id="frm" name="frm" action="#">
    <div id="qtyElems">
      <span>First: </span><input id="qtyOne" name="qtyOne" value="5">
      <br>
      <span>Second: </span><input id="qtyTwo" name="qtyTwo" value="5">
      <br>
      <input type="button" 
        onclick="calculateForPattern(this.form, 'qty');" value="Calculate!">
    </div>
  </form>
</body>
</html>

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.