Jump to content

Display number of html elements on demand


Recommended Posts

requires jQuery...anyways, I'm not sure if you meant that you want only to dynamically append selected form elements, or if there are X amount you want to append for every row...I went with the former because that's harder.  If it's the latter, this code can be simplified.

 

<form id='someForm' action='' method='post'>
  <!-- row options to (multi)select -->
  <select id='rowOptions' name='rowOptions' multiple="multiple" size="3">
    <option value="textField">text field</option>
    <option value="radioField">radio button</option>
    <option value="textAreaField">text area</option>
  </select>
  <br>
  <!-- button to dynamically append new form fields when clicked -->
  <input type='button' value='add row' id='addRow' />
  <input type='submit' value='submit' id='submit' />
</form>

<script type='text/javascript' src='jquery.js'></script>
<script type='text/javascript'>

// max number of rows to append
var maxRows = 20; 
// current row to append
var rowNum = 0;

$(document).ready(function() {
  // on click of the 'add row' button...
  $('#addRow').click(function() {
    // if the current row is less than max rows ...
    if (rowNum < maxRows) {
      // variable to hold stuff to append 
      var divRowContent = '<div id="rowDiv'+rowNum+'" style="border:solid green 1px;">';
      // get the selected options
      var selected = $('#rowOptions').val();
      // for each option selected...
      for (var x=0; x < selected.length; x++) {
        // add the form element to variable 
        switch (selected[x]) {
          case 'textField' :
            divRowContent += '<input type="text" name="textField['+rowNum+']" id="textField'+rowNum+'" /><br>'; 
            break;
          case 'radioField' : 	
            divRowContent += '<input type="radio" name="radioField['+rowNum+']" id="radioField'+rowNum+'_a" value="a" />a';
            divRowContent += '<input type="radio" name="radioField['+rowNum+']" id="radioField'+rowNum+'_b" value="b" />b<br>';
            break;
          case 'textAreaField' : 
            divRowContent += '<textarea name="textAreaField['+rowNum+']" id="textAreaField'+rowNum+'" rows="2" cols="20"></textarea><br>';
            break;
        } // end switch
      } // end for
      // closing div tag for the appending row
      divRowContent += '</div>';
      // add row
      $('#someForm').append(divRowContent);
      // increment row counter 
      rowNum++;
    } // end if
  }); // end .click
}); // end .ready
</script>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.