Unexpected trigger of Bootstrap modal

Currently, I have a button on my webpage that is triggering a bootstrap modal to open, but I am not ready for that yet. Here is the HTML code for the button:

<div class='bookmarkButtonDiv'>
   <button  type='button' class="btn btn-primary" data-toggle='modal' data-target='#newModal' id="IBtn">Bookmark</button>
</div>

Here is the code for my modal:

<div class="modal fade" id="newModal" role="dialog">
<!-- some code to populate the modal -->

In my JavaScript, I have:

jQuery.noConflict();
(function($) {
  $(function () {
    $('#IBtn').click(function (e) {
      console.log('button clicked');
     });
    });
  })(jQuery);

However, when clicking the button, it opens the bootstrap modal and logs a message to the console. What I actually want is just the console log. My ultimate goal is to have the button trigger the modal, but right now, I'm testing things in the console only. I am using Bootstrap version 4.3.

Answer №1

It is recommended to remove the

data-toggle='modal' data-target='#newModal'
attributes as Bootstrap automatically handles opening the modal when elements with those attributes are clicked.

jQuery.noConflict();
(function($) {
  $(function () {
    $('#IBtn').click(function (e) {
      console.log('button clicked');
    });
  });
})(jQuery);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>

<div class='bookmarkButtonDiv'>
  <button type='button' class="btn btn-primary" id="IBtn">Bookmark</button>
</div>
<div class="modal fade" id="newModal" role="dialog">
  <h1>Modal contents</h1>
</div>

Answer №2

To accomplish your goal, there are two methods at your disposal

Initial Option

Erase

data-toggle='modal' data-target='#newModal'

Secondary Approach

$('#IBtn').click(function (e) {
    e.stopPropagation();
    console.log('button clicked');
});

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

What steps should I take if the slackbot is properly functioning after being invited to the channel?

Using an OAuth2 token I am looking to automate the process of sending data from Google Sheets via Slackbot. Even though I have set up tokens and connections, I find that I still need to manually input the channel id into my script. In order to streamline ...

Developing a versatile code block for handling animations within my project

As I delve into learning jQuery to enhance my new website, I realize that my expertise lies more in web design rather than programming. I find myself struggling to create a versatile function that can be applied to multiple div elements within the HTML. B ...

Jquery(Ajax) call encountered a problem when attempting to send the object

Struggling to send data to a MongoLab DB using JS and JQueryAjax call. The issue lies in being able to see the OID in the DB but no data is actually populated. Upon checking the code and network settings, it's evident that the payload always remains e ...

Contrasting assign and modify operations on arrays

After spending 2 days searching for a bug in my angular2 project's service.ts file, I finally found it and fixed it. However, I'm still trying to understand why the working code behaves differently from the bugged one, as they appear identical to ...

Can I use Mongoose to apply a filter to this request?

I have a requirement to query all products with the category status set to true. My product model looks like this: const ProductSchema = Schema({ name : {type: String}, category: {type: Schema.Types.ObjectId, ref: 'Category'} }) Normally, I wou ...

Attempted to create registrations for two views using the identical name RCTScrollView

Having trouble running my React Native app on iOS, I keep getting an error while the Android version works perfectly fine. Does anyone have any insight on this issue? XCode 11.5, RN 0.61.5, Using React Native CLI I've searched multiple sites but hav ...

What is the best way to format a JavaScript datetime to show in 12 hour AM/PM style?

What is the best way to format a JavaScript datetime object in the 12-hour clock (AM/PM) format? ...

Sending form information through AjaxPassing information from a form using

Currently, I am working on a project where one page opens a thickbox of another page that contains a form. Once the form is submitted and data is written to the database, I need the parent page of the thickbox to update specific rows of the form that have ...

Randomly reorganize elements in a JavaScript array

I am facing an unusual issue while shuffling an array in JavaScript and I am unable to identify the root cause. Can someone provide assistance? When attempting to shuffle an array, the output I receive is unexpected: [1,2,3,4,5,6,7,8,9,10] Instead of ...

Module or its corresponding type declarations not found in the specified location.ts(2307)

After creating my own npm package at https://www.npmjs.com/package/leon-theme?activeTab=code, I proceeded to set up a basic create-react-app project at https://github.com/leongaban/test-project. In the src/index.tsx file of my react app, I attempted to im ...

The function Server.listeners is not recognized by the system

Currently, I am following a tutorial on websockets to understand how to incorporate Socket.IO into my Angular project. Despite meticulously adhering to the instructions provided, I encountered an error when attempting to run my websockets server project: ...

What is the best way to pass template variables in Meteor?

I couldn't find a solution for this particular issue, although I have encountered it in the past. The challenge is to render a template with a variable set from HTML and also be able to access it in JavaScript. Here's a straightforward example t ...

Java REST service remains out of reach for JavaScript fetch call

Currently, I am in the process of learning about REST API. My goal is to make a call to a POST service implemented in Java from Javascript using fetch. However, I have encountered an issue where the request fails to reach the service whenever the @Produces ...

Using Selenium Webdriver to open a JavaScript calendar popup window

While testing a basic website, I encountered an issue with the Firefox webdriver not displaying a Javascript calendar window when a button is clicked. Even though the Selenium IDE successfully spawns the window, running the Java code does not result in the ...

Using PHP to create an HTML text box that calculates the total amount by multiplying the quantity with the

$res=mysql_query($qry); while($row= mysql_fetch_array($res)) { echo "<tr><td>".$row['Food_Name']."</td> <td>".$row['Price']."</td> <td><input type='text' name='qty". $row['code& ...

Generate a cytoscape hierarchy using PHP script

Could someone please take a look at my code? I'm attempting to display the results of a query with a graph using cytoscape. Since I'm not very experienced with JavaScript, there might be a syntax error. <script type="text/javascript"> ...

Leveraging the power of Ajax in JavaScript

For my assignment, I successfully made two separate paragraphs hide and show using jQuery. Moving forward, the next step involves integrating JavaScript Ajax code to allow the paragraphs to toggle visibility. To achieve this, I created two distinct HTML fi ...

Instructions on displaying a pop-up message upon clicking the edit button with the use of javascript

How can I display a popup message box when the edit button is clicked on my PHP page? I want to confirm before passing data to the edit page, but currently, the data is being sent without displaying any message. Any suggestions on how to fix this issue w ...

What is the best way to construct a collection of this specific data structure and transmit it using socket.io?

I am currently using node.js along with socket.io in my project. Within my MySQL table, I have the following data structure: TABLE USERS: Id: 1, Name: 'ABC1', ToUserId: 1 Id: 2, Name: 'ABC2', ToUserId: 1 Id: 3, Name: &apos ...

Implementing Express.js allows for the seamless casting of interfaces by the body within the request

I have created a similar structure to ASP.NET MVC and uploaded it on my github repository (express-mvc). Everything seems fine, but I am facing an issue with casting the body inside the request object to match any interface. This is what I am aiming for: ...