How to add Controls Dynamically in RoR?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dakshayini
    New Member
    • Dec 2007
    • 16

    How to add Controls Dynamically in RoR?

    The problem is, i have to add the controls dynamically in html/ruby html which should take the unique id for each controls.
    Eg: If i click on add button say,control should create the code as follows..

    Code:
    <input type ="text" name="text1" id="text1">
       <input type ="text" name="text2" id="text2">...
    How can i achieve this?Please help.Its urgent
    Thanks
  • improvcornartist
    Recognized Expert Contributor
    • May 2007
    • 303

    #2
    You can use javascript to add new elements. Something like
    Code:
    function addElement() {
      var div = document.getElementById('div1');
      var element = document.createElement('input');
      element.setAttribute('type', 'text');
      element.setAttribute('name', 'text1');
      element.setAttribute('id', 'text1');
      div.appendChild(element);
    }
    You will need to set the name and id dynamically, maybe by using a counter or something from your ruby code.

    Comment

    Working...