Reputation: 325
I want to make a modal-cookie thing where on page load, the modal checks if there is a cookie, and then if there is no "name" cookie, the modal will show. otherwise, the modal won't show. However, I have this error:
Uncaught SyntaxError: Identifier 'addCookie' has already been declared.
Because of this error, my cookies do not save.
I understand that this error happens when someone is trying to override a let
. However, I didn't try to override it, I just tried to call it
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>
<div id="modalWelcome" class="modal fade" data-backdrop="false">
<div class="form-group">
<input type="text" class="form-control" placeholder="your name" id="fname">
</div>
<button type="submit" class="btn btn-primary" onclick="addCookie()">go!</button>
</div>
if ($.cookie('name') == undefined) {
$("#modalWelcome").modal('show');
} else {
$('#modalWelcome').modal('hide');
}
var cookiename;
let addCookie = () => {
cookiename = $('#fname').val();
$.cookie("name", cookiename);
}
Upvotes: 0
Views: 7821
Reputation: 8572
onclick="addCookie()"
to onclick="addCookie"
because you need to set the handler, not execute.Upvotes: 1