Reputation: 367
How can I trigger a button in HTML using the javascript?
I tried to use the <script></script>
the HTML file and it works perfectly fine, but then I want to use a javascript file to trigger when clicking a button in HTML.
here is the code:
JAVASCRIPT in HTML <head></head>
tag:
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js">
<script src="jsFiles/index.js"></script>
HTML:
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Message</a>
<a class="dropdown-item" href="#">Settings</a>
<a class="dropdown-item" id="a_id">Log Out</a>
</div>
JAVASCRIPT FILE:
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
$(document).ready(function(){
$("#a_id").click(function(){
swal({title:'Logout',
text:'Do you want to logout this Account?',
icon:'warning',
buttons: true,
dangerMode: true
})
.then((willOUT) => {
if (willOUT) {
window.location.href = 'page-logout.php', {
icon: 'success',
}
}
});
});
});
</script>
Upvotes: 3
Views: 62
Reputation: 33726
You should remove everything is not javascript code, so your file index.js
should look as follow:
$(document).ready(function() {
$("#a_id").click(function() {
swal({
title: 'Logout',
text: 'Do you want to logout this Account?',
icon: 'warning',
buttons: true,
dangerMode: true
})
.then((willOUT) => {
if (willOUT) {
window.location.href = 'page-logout.php', {
icon: 'success',
}
}
});
});
});
Upvotes: 2