Append Row From User Input With Javascript
Solution 1:
Since nobody else wants to (or can) provide a pure JS answer, here's how you can do it with pure JavaScript. If some things are unclear, let me know and I'll be happy to provide you with useful links and explain what this code does:
HTML:
<tableid='targetTbl'><tbody><tr><th>Name</th><th>First name</th></tr></tbody></table><formid='inForm'><inputtype='text'name='name'id='name'/><inputtype='text'name='first'id='first'/><inputtype='submit'value='add row'/></form>
JavaScript:
document.getElementById('inForm').onsubmit = function(e)
{
var newRow,i;
e = e || window.event;
if (e.preventDefault)
{
e.preventDefault();
e.stopPropagation();
}
e.returnValue = false;
e.cancelBubble = true;
newRow = '<tr>';
for(i=0;i<this.elements.length;i++)
{
if (this.elements[i].tagName.toLowerCase() === 'input' && this.elements[i].type === 'text')
{
newRow += '<td>'+this.elements[i].value+'</td>';
}
}
document.getElementById('targetTbl').innerHTML += newRow + '</tr>';
returnfalse;
};
In order for this to work, you need to either include this code at the bottom of your HTML file (not reccomended) to ensure the DOM has loaded before you're trying to change things, or you'll have to put this code inside an event handler:
window.onload = function()
{
//above code goes here
};
Solution 2:
Few points to consider:
You do not want your submit button to work. That means you do not want it to take you to the
action
you specified in the form element. So you either add an onclick event which (<input type="submit" onclick="dosomething();return false;" />
) returns false, or you make it of type "button" (<input type="button" onclick="dosomething();" />
).You need to define a function that should be called when the button is pressed:
Example:
function dosomething() {
alert("I did something");
// do something useful
}
- You need to create a
<table>
tag, and add rows to it based on your data.You could use jQuery for that, and I would recommend that to get you starting quickly. DOM manipulation in javascript is a pain.You know what, forget jQuery. Check @Elias's answer for a better, cleaner, faster approach. And also, it teaches you much more. It might be a valuable asset when/if you do use a library, and it certainly will, if you can do without one.
Example (in jQuery):
var table = $("<table></table>");
for (var i=0; i<data.length; i++) {
var tr = $("<tr></tr>");
var td = $("<td></td>");
$(td).text(data[i]);
$(tr).append(td);
$(table).append(tr);
}
$("body").append(table);
Solution 3:
Ok Guys, After reading some more and asking a friend to explain, i was able to do this with jQuery. I am sorry I didnt add jQuery tag at the beginning, because I didn't know. But I was exposed to jQuery from the very first exercise in js. So I was able to grasp this eventually. So this is what I have and it works for me. If this is right, might be useful for a learner like me.
The ids of my input fields are "firstname" and "lastname". getElementById().value gets the value input by the user.
var firstname = document.getElementById("firstname").value;
var lastname = document.getElementById("lastname").value;
$("#myTable tr:last").after("<tr><td>" + firstname + "</td><td>" + lastname + "</td></tr>" );
And I am calling this function through button onclick(). Thank you for the support.
Solution 4:
You have two choices:
Classical: You submit the form. The server reads the form, and generates the HTML for a new copy of the form with the new row added and sends it back to the browser.
Ajax: You submit the form in the background using AJAX, then using JavaScript, you append a new row to the HTML client-side.
Which you choose depends on your comfort level with back-end vs. front-end coding.
Solution 5:
You can use this function. You need to send as a parameter the id of the table and change valueInputForm with the variables in which you have the values of the inputs
functionaddRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
cell1.innerHTML = valueInputForm;
var cell2 = row.insertCell(1);
cell2.innerHTML = valueInputForm;
var cell3 = row.insertCell(2);
cell2.innerHTML = valueInputForm;
}
Post a Comment for "Append Row From User Input With Javascript"