0

I'm trying to make a button on an HTML form add elements to the form:

function createRow(){
    var row = document.createElement('div');
    var input=document.createElement('input');
    input.setAttribute("type", "Textbox");
    var time=document.createElement('input');
    time.setAttribute("type", "Textbox");
    row.appendChild(input);
    row.appendChild(time);
    return row;
}
function addRow(){
    console.log("z");
    var routes = document.getElementById("routes");
    routes.appendChild(createRow());
    return false;
}
<form id="routes">
    <button onclick="addRow()">+</button>
</form>

But on each button click the page reloads with a question mark added to url. I've found a pair of ideas:

  1. preventDefault(e)
  2. return false

but none worked.

Please tell me what shall I fix? Also there are some conditions:

  1. This is not the submit button, which will be added later
  2. It's required to keep it as simple as possible, the interface is not main focus of project
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
SubZr0
  • 137
  • 7
  • Possible duplicate of [How do I make an HTML button not reload the page](http://stackoverflow.com/questions/1878264/how-do-i-make-an-html-button-not-reload-the-page) – pvg Mar 07 '17 at 18:20

5 Answers5

0

The default behavior of button is to submit. Specify the type=button and it will override this default behavior.

<button type="button" onclick="addRow()">+</button>

Rick S
  • 6,476
  • 5
  • 29
  • 43
0

The onclick function should return false. Also addRow() needs to return false:

<form id="routes">
   <button onclick="return addRow()">+</button>
</form>
Luca Jung
  • 1,440
  • 11
  • 25
0

Ok, there is example of using preventDefault on event listener.

 function createRow(){
    var row = document.createElement('div');
    var input=document.createElement('input');
    var time=document.createElement('input');
    input.setAttribute("type", "Textbox");
    time.setAttribute("type", "Textbox");
    row.appendChild(input);
    row.appendChild(time);
    return row;
}

function addRow(){
    document.getElementById("routes").appendChild(createRow());
}

document.querySelector("button").addEventListener("click", function(event)
{
  event.preventDefault();
  addRow();
});
<form id="routes">
    <button>+</button>
</form>
Profesor08
  • 1,181
  • 1
  • 13
  • 20
0

Inside of a form an HTML5 button element has a default type of submit. If you give it a type of "button" everything works fine

<!DOCTYPE html>
<html>
 <head>
 </head>
 <body>
 <form id="routes">
     <div><button type="button" onclick="addRow()">+</button></div>
 </form>
 <script type="text/javascript">
     function createRow(){
         var row = document.createElement('div');
         var input=document.createElement('input');
         input.setAttribute("type", "Textbox");
         var time=document.createElement('input');
         time.setAttribute("type", "Textbox");
         row.appendChild(input);
         row.appendChild(time);
         console.log(row);
         return row;
     }
     function addRow(){
         console.log("z");
         var routes = document.getElementById("routes");
         routes.appendChild(createRow());
         return false;
     }
 </script>
 </body>
 </html>
jkollin
  • 13
  • 2
  • 6
0

Give button a id and put preventDefault inside $(document).ready(..) function

    $(document).ready(function(){
 
  $( "#routes_button" ).click(function( event ) {
   event.preventDefault();  
  });
 });
 
 function createRow(){
        var row = document.createElement('div');
        var input=document.createElement('input');
        input.setAttribute("type", "Textbox");
        var time=document.createElement('input');
        time.setAttribute("type", "Textbox");
        row.appendChild(input);
        row.appendChild(time);
        return row;
    }
    function addRow(){
        console.log("z");
        var routes = document.getElementById("routes");
        routes.appendChild(createRow());
        return false;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>


<form id="routes">
        <button id="routes_button" onclick="addRow()">+</button>
    </form>
Rafiqul Islam
  • 1,636
  • 1
  • 12
  • 25