Dennis Dennis - 1 year ago 90
Javascript Question

JS - creating table dinamically

<!DOCTYPE html>
<link rel="stylesheet" href="" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<link href="mycss.css" rel="stylesheet" type="text/css">

<link rel="stylesheet" href="">

<script src=""></script>
<script src=""></script>

<script src="" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>


<div class="container">

<div class="row">
<div class="col-md-6 col-md-offset-3">
<p><small>Please, fill in all the fields.</small></p>

<form id="register-form">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" placeholder="Enter user name" required>

<div class="form-group">
<label for="dof">Date of Birth</label>
<input type="date" class="form-control" id="dof" placeholder="mm/dd/yyyy" required>

<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" placeholder="Enter email">

<div class="form-group">
<label for="tel">Phone number</label>
<input type="tel" class="form-control" id="tel" placeholder="Enter Phone number">

<button id="save-button" class="btn btn-primary pull-right" onclick="save();">Save</button>


<div class="row">
<div class="col-md-6 col-md-offset-3">
<table class="table">
<th>Date of Birth</th>
<th>Phone number</th>


$( function() {
$( "#dof" ).datepicker();
} );

<script type="text/javascript">
function save() {
var tbody = document.getElementsByTagName('tbody')[0];
var name = document.getElementById('name').value;
var dof = document.getElementById('dof').value;
var email = document.getElementById('email').value;
var tel = document.getElementById('tel').value;

var tr = document.createElement('tr');
var columns = [name, dof, email, tel]

for (var i = 0; i < columns.length; i++) {
var td = document.createElement("td");
var text = document.createTextNode(columns[i]);





My save() function doesn't work. I would be very thankful for any ideas!

P.S. Guys :) do not read it..this is for support service ;) It is annoying a lot.. I mean the popup that says that It looks like mostly code...bla bla

Answer Source

Your problem is that the form submits, thereby reloading the page and resetting everything.

This you can solve by calling preventDefault() on the event object. For that you have to change two things to the save function:

  1. Add a function argument to capture the event object, let's call it e:

    function save(e) {
  2. Call preventDefault somewhere in the function, but it is common to put this near the end:


This is not all. Now you have to make sure the event object is passed to your function. There are at least two ways:

  1. If you stick to the onclick attribute, then change it to include the special event object:

    <button ... onclick="save(event);">
  2. Many regard it better practice to not use the onclick attribute at all. In that case remove that attribute, and attach the event handler through JavaScript:


    In that case the event object is passed automatically.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download