1

Hello I am trying to add multiple textbox when click on button. i have write following HTML code.

<script type="text/javascript">
 function addRow(btn) {         
     var parentRow = btn.parentNode.parentNode;
     var table = parentRow.parentNode;
     var rowCount = table.rows.length;
     var row = table.insertRow(rowCount);
     var cell1 = row.insertCell(0);
     var element1 = document.createElement("input");
     element1.type = "text";
     cell1.appendChild(element1);
     var cell3 = row.insertCell(1);
 }
</script>
<table>
   <tr>
      <td><input type="text" name="data1" value="abc" /></td>
      <td><button type="button" onClick ="addRow(this)">Add</button></td>
   </tr>
</table>

i dont know how to do . kindly tell me how to do this stuff .... thank you in advance

8
  • what is happening, any errors, is the function being called? Can you check using firebug? Commented Oct 2, 2013 at 5:49
  • no i just try in wecschool may be function is not call Commented Oct 2, 2013 at 5:52
  • @kapil you want this jsfiddle.net/cse_tushar/9EKRB or jsfiddle.net/cse_tushar/9EKRB/1 ?? Commented Oct 2, 2013 at 5:54
  • @AjinderSingh i have already write like thiks onClick ="addRow(this)" Commented Oct 2, 2013 at 5:56
  • 1
    @TusharGupta woww... thats great Commented Oct 2, 2013 at 5:57

2 Answers 2

3

Wrap your JavaScript code in the head section.

DEMO of your code

same code with jQuery

DEMO

var txtbox = '<td><input type="text"/></td>';

function addRow(btn) {
    $(btn).closest('tr').append(txtbox);
}

DEMO

var txtbox = '<tr><td><input type="text"/></td></td>';

function addRow(btn) {
    $(btn).closest('table').append(txtbox);
}

References

.closest()

.append()

To add Drop-down list

you just need to add dropdown list code in the var txtbox

DEMO

var txtbox = '<tr><td><select><option value="volvo">Volvo</option><option value="saab">Saab</option><option value="mercedes">Mercedes</option><option value="audi">Audi</option></select></td></td>';

function addRow(btn) {
    $(btn).closest('table').append(txtbox);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hello i have use this in my page but it append end of the table .. i want to add after particular div. then tell me how to do
2

try this sample...

var emails = document.getElementById('emails'),
 add_link = document.createElement('a'),
 template = emails.getElementsByTagName('div'),
 current = template.length,
 max = 20;
 template = template[0];

 submit1.onclick = function () {
 var new_field = template.cloneNode(true);
 current += 1;
 new_field.innerHTML = new_field.innerHTML.replace(/1/g, current);
 emails.appendChild(new_field);
if (current === max) {
    add_link.onclick = null;
    document.body.removeChild(add_link);
}
return false;
};

document.body.appendChild(add_link);

For demo http://jsfiddle.net/wQfLT/145/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.