Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Button redirects pages

I have the following button:

<div class="form-group">
    <button onclick="assign_all()" class="btn btn-default">Vælg alle</button>
</div>

Now this calls a JavaScript function called assign_all

This works fine however when it is done executing the JavaScript it simply tries to load a page. Which is fairly odd (its like if you click on an A tag)

As you can see below nothing in my javaScript indicates that it should reload / load a page:

function assign_all() {

    var info = [];
    var i = 0;
    $('.user_list').each(function () {
        if ($(this).hasClass('show')) {
            info[i] = $(this).find('.user_id').val();
            i++;
        }
    })
}

Can anyone tell me why this is happening?

like image 812
Marc Rasmussen Avatar asked Jun 30 '14 11:06

Marc Rasmussen


1 Answers

It's because the default button element's type attribute is set to submit. Your button element is attempting to submit a form. Simply give it a type of button:

<button onclick="assign_all()" class="btn btn-default" type="button">...</button>
like image 91
James Donnelly Avatar answered Sep 25 '22 20:09

James Donnelly