"Encountering an issue with a Bootstrap button that won't respond to a simple onclick event

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"

Answer №1

Problem solved:

After troubleshooting, I discovered an error in the script (window.alert was misspelled as windowalert)

<script>
function loginfunction() {
  window.alert("Login functionality is under development.");
}
</script>   

Additionally, I forgot to include a semicolon...

<button type="button" onclick="loginfunction();" class="btn btn-secondary">Login</button>

Interestingly, the placement of the JavaScript code did not impact its functionality - it worked both above and below the button element.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Where, oh where, has the text-center in Bootstrap disappeared to?

Does anyone know where the text-center (formerly text-xs-center) has disappeared to in this latest update? Has it been renamed once more? If yes, what is it called now and in which sass module can I locate it? ...

Tips for placing the header text at the center of a TemplateField in your design

I'm using a GridView with TemplateFields. I attempted to align the header text of the TemplateField to center by using HeaderStyle-HorizontalAlign="Center", but it doesn't seem to be working as expected. <asp:TemplateField HeaderTex ...

Opening a new tab on any webpage with C# Selenium

Is there a foolproof way to open a new tab when using Selenium? Trying to use an action method has proven unsuccessful Actions a = new Actions(driver); a.KeyDown(OpenQA.Selenium.Keys.LeftControl); a.SendKeys("t"); a.KeyUp(OpenQA.Selenium.Keys.L ...

Modify Javascript to exclusively focus on SVG paths

I successfully created an SVG animation using JSFiddle, but when I transferred the SVG to Shopify, the Javascript that animates the path stopped working. It seems like the issue is with the JavaScript targeting all paths on the page instead of just the sp ...

Enhancing a child object using Knockout Mapping

Take a look at this JSON object: { "BusinessModels":[ { "Id":1, "Name":"Business to Business", "Businesses":[], "ReportTypes":[ {...}, {...}, { ...

Can .NET MVC be used to host vue.js static files?

I recently received a sample Vue app created using vue-cli, which generates static files when the npm run build command is executed. Normally, these files are served by a server from node.js. However, I am curious to know if it's possible to serve th ...

What is the benefit of using an IIFE in this particular way when constructing a JavaScript library?

Many libraries I've seen use a similar style to define their structure. Upon further inspection, I've noticed that the first self-invoking function is often related to Require.js or AMD systems, with 'factory' as an argument. My curiosi ...

What could be the reason for the "update State" button not updating the state within the superclass to the sliced array, but instead, it only logs the entire array in the console

Looking at the image provided https://i.sstatic.net/X4cSX.png I am puzzled as to why, upon clicking the updatestate button, the state does not update to the sliced array ['usa', 'china']. Instead, it updates the entire array in console ...

A guide on incorporating requireJS for browser using npm installation

When it comes to utilizing npm-installed modules with requirejs, a common question arises: how do I actually use requirejs if it's installed via npm install requirejs in the first place? I've gone through examples of using requirejs provided in ...

The slide effect in jQuery's show() function isn't being displayed as expected

How can I implement a slide effect to hide and show an article element based on different navigation clicks? When loading the page, clicking on Home smoothly hides the article with no issues. However, when clicking on the About Us link, the article is imme ...

Tips for optimizing scrolling of a specific element with a letter parameter

For my web application project, I am trying to implement a feature where clicking on a letter will scroll to display the relevant content. Below is the JavaScript code snippet: <script> import Bscroll from 'better-scroll' export default ...

Identifying page dimensions and implementing CSS adjustments when the width decreases to less than 500 pixels

I'm working on a website and I need to create a script that can detect when the page width is greater than 500px or less than 500px so I can adjust the layout accordingly. At the bottom of the page, there are some links arranged like a footer. When th ...

Pass the Vue `v-for` loop index as an argument to a click

How can I pass the index of a v-for loop as an argument to a click handler without it returning undefined? <div v-for="(item, key, index) in groups" v-on:click="selected(index)">{{item.name}}</div> Here's my Handler: selected(i) { c ...

What is the method for accessing a marker from beyond the map on OpenStreetMap?

Recently, I made the switch from using Google Maps to OpenStreetMap in my project due to the request limit constraints imposed by Google. My client needed a higher request limit, but was unable to afford the costs associated with increasing it on Google Ma ...

Fetching data from database and populating dropdown menu through Struts 2 action class with the assistance of jtable

Utilizing the jtable jQuery plugin (http://jtable.org/) in my current project has been a game-changer. It allows for the creation of AJAX based CRUD tables without the need to code HTML or JavaScript. For more details, refer to this image: The jtable cod ...

Implementing meta tags in React.js

I am attempting to incorporate dynamic meta-tags on each page of my website. However, despite my efforts, I cannot seem to locate the meta-tags in the page's source code. Do I need to make adjustments in public/index.html, considering that I am not ut ...

Creating a blank array in NodeJs using mongoose

I'm currently facing an issue while attempting to populate an array with records from a mongoDB database using mongoose. Even though I declare the array outside the function, it appears empty when logged outside the function. Here's the code snip ...

Highlighting menu borders and images upon loading of the page

Currently, I am working on creating a unique menu design where the initial link is highlighted with a border-bottom when the page loads. Additionally, there is an image that follows the mouse when hovering over each link in the menu. Although I have succe ...

Tips for transferring a value in JavaScript

There seems to be an issue with NULL VALUE ON $_SESSION in my compare.php file. Any ideas on how to resolve this? Thank you Below is the JavaScript function: $(function() { $('input[type=checkbox]').change(function() { var chkAr ...

Pause the YouTube video and swap it out with an image

I am currently working on incorporating an embedded YouTube video into my project. I want to replace the video with an image, and when the image is clicked, the video should start playing. I have successfully implemented this functionality: <script typ ...