javascript - add tr immediately after the button

715
933

Answer

Solution:

You can use jQueryAfter(); jquery.com/after/ and a click event, and make sure the input name is an array[]

$('.addme').click(function(){
     $('.uploads').last('tr').after('<tr align=center ><td colspan="4"><input type="file" name="oc[]"/></td></tr>');
});

http://jsfiddle.net/j0jjzm7o/16/

671

Answer

Solution:

jQuery can do this for you using.append() - jquery.com/append

An untested example...

Here is the HTML,

<table id="table">
  <tr>
    <td><input type="file" name="file[]" /></td>
  </tr>
</table>
<button id="button">Add another</button>

and the javascript,

$("#button").click(function(){
  var string = '<tr><td><input type="file" name="file[]" /></td></tr>';
  $("#table").append(string);
});
856

Answer

Solution:

You will need some JavaScript(JQuery) to accomplish your task.

Live Preview: http://codepen.io/larryjoelane/pen/Nxovdj

Place the following in a script tag before your closing body tag or load it from an external file after you load JQuery. If you are unsure how to load JavaScript through an external file you can download the example from this link and explore the index.html file.

Example Download

//store the addme id
var addMe = "#addme";

//store the tr markup
var tr = '<tr align=center ><td colspan="4"><input type="file" name="oc" id="oc"/></td><td><input type="button" value="add more" id="addme" /></td></tr>';

//add the on click event
$(document).on("click",addme,function(){


  //select the table
  var table = $("#table"); 

  //append the tr
  table.append(tr);


});

People are also looking for solutions to the problem: php - How to pass the search results specific component session into another page

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.