Issue
I want to trigger some JavaScript code when the button is clicked.
I want that when the button is clicked console.log()
is triggered.
My attempt:
corel.html :
<!DOCTYPE html>
<html>
<head>
<title>Corelation</title>
<script src="jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="corel.js"></script>
</head>
<body>
<form id="tableVar">
Variable 1: <input type="text" id="var1"><br>
Variable 2: <input type="text" id="var2"><br>
<button onclick="myFunction()">Click me</button> <!-- type is button to not refresh -->
</form>
</body>
</html>
corel.js :
console.log("iniating app ...");
function myFunction() {
console.log('yeah');
}
I do not understand what does not work ?
Solution
The button is making the form submit hence you need to use preventDefault()
Use this code,
HTML
<!DOCTYPE html>
<html>
<head>
<title>Corelation</title>
<script src="jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="corel.js"></script>
</head>
<body>
<form id="tableVar">
Variable 1: <input type="text" id="var1"><br>
Variable 2: <input type="text" id="var2"><br>
<button onclick="myFunction(event)">Click me</button> <!-- type is button to not refresh -->
</form>
</body>
</html>
Corel.js
console.log("iniating app ...");
function myFunction(event) {
console.log('yeah');
event.preventDefault();
}
Answered By - Saumil
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.