Within my navigation bar, I have included the following Bootstrap button:
<button type="button" onclick="loginfunction()" class="btn btn-secondary">Login</button>
Beneath the button, you will find the corresponding JavaScript function:
<script>
function loginfunction() {
windowalert("Login functionality is under development.");
}
</script>
While I am familiar with calling a function from a button, such as the example shown below:
<!DOCTYPE html>
<html>
<body>
<button onclick="this.innerHTML=Date()">The time is?</button>
</body>
</html>
I encountered an issue due to the additional existing code in this case and was unsure of the correct syntax.
For the specific snippet of code:
<button type="button" onclick="loginfunction()" class="btn btn-secondary">Login</button>
I wondered if I needed to remove 'button type="button"' and code directly? I attempted various approaches but kept encountering errors.
As an example, I tried the following without success, which also resulted in losing the Bootstrap styling as I removed the class information:
<button type="onclick"=loginfunction() class="btn btn-secondary">Login</button>
The complete contextual code, including the navbar with the login button and associated script for the loginfunction onclick event, is provided below.
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="#"><Vsiteclass="tk"></VSite></a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarText">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Symptom checker</a>
</li>
</ul>
<span class="navbar-text">
<button type="onclick"=loginfunction() class="btn btn-secondary">Login</button>
<script>
function loginfunction() {
windowalert("Login functionality is under development.");
}
</script>
<button type="button" class="btn btn-danger">Signup</button>
</span>
</div>
</div>
</nav>
To preserve the Bootstrap button with its styled class, it is necessary to retain the following:
<button type="button"