0

I have a simple button that executes a javascript function on click. The button seems to be refreshing the page, and the js code is not executing at all.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Dynamic Form</title>
</head>
<body>

<div class="col-sm-12 blog-main">
    <form class="form-addCards form-inline" role="form">
        <div class="col-xs-2" id="submitBtn">
            <button class="btn btn-primary" onclick="submit();">Submit</button>
        </div>
    </form>
</div>

<script src="newSet.js"></script>
</body>
</html>

And the js:

function submit(){
    event.preventDefault();
    alert("hit");
}
Mardymar
  • 419
  • 3
  • 14
  • Have you checked the debugger for errors? `event` is probably not defined, since you're not passing it as an argument to the function. – Brian Shamblen Mar 06 '16 at 00:04
  • `event` isn't a global variable (anymore). It's an argument passed to the event handler. With `onevent` attributes, you'll have to pass it along to any functions that need to use it – `onclick="submit(event)"`. And, have them accept it – `function submit(event)`. – Jonathan Lonowski Mar 06 '16 at 00:07

1 Answers1

-2

I think its,becuse submit is a built in function for the form. Try calling your function something else, or add id to your button and attach onclick using jquery on method.

$('#buttonid').on('click', function() { // do stuff });

Phil
  • 4,029
  • 9
  • 62
  • 107