If the user confirms it with a bootstrap dialog box, they will be redirected to the corresponding links

Firstly, I am not interested in using javascript confirm for this purpose. Please read on. For example, adding an onClick attribute to each link with a function that triggers a system dialog box is not what I want.

Instead of the standard system dialog box, I have implemented bootstrap's dialog box.

Here are several delete buttons for different items:

<a href="www.mysite.com/product/1" data-target="#myModal"  data-toggle="modal"><i class="icon-remove"></i> Delete</a>
<a href="www.mysite.com/product/2" data-target="#myModal"  data-toggle="modal"><i class="icon-remove"></i> Delete</a>
<a href="www.mysite.com/product/3" data-target="#myModal"  data-toggle="modal"><i class="icon-remove"></i> Delete</a>

Below is the code for displaying the modal using Bootstrap:

<div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Confirm Delete ?</h3>
  </div>
<div class="modal-body">
  <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to Delete the Category and all its associated Properties?</p>
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
    <button class="btn btn-danger" data-dismiss="modal">Delete</button>
  </div>
</div>

The dialog box is working correctly, but after clicking the delete button in the dialog box, it does not redirect to the corresponding page of the links above. The modal simply disappears without redirection.

Answer №1

To easily remove items, simply assign the class remove-item to your links and include this script on your webpage for a seamless process.

<script>
$(function () {
  $('a.remove-item').click(function () {
    var url = this.href;
    $('#myModal .btn-danger').click(function () {
      window.location.href = url;
    });
  });
});
</script>

Answer №2

To enable the deletion of items without refreshing the page, you can include data-attributes in your bootstrap markup and make an AJAX request to the server.

<div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Confirm Delete ?</h3>
  </div>
<div class="modal-body">
  <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to Delete the Category and all its associated Properties?</p>
  </div>
  <div class="modal-footer">
    <a class="btn" data-dismiss="modal" aria-hidden="true">Cancel</a>
    <a class="btn btn-danger" data-action="delete-item" data-item="XXX" data-dismiss="modal">Delete</a>
  </div>
</div>

Using jQuery or JavaScript, you can then send a request to the server for deleting the data-item specified as XXX.

You can simplify the process of dynamically adding data-items by generating them using a JavaScript method like below:

function show_dialog_for(item_id)
{
   //your code here to generate and display the modal
   //Bind the ajax method to send the delete request for the item_id
   $("a[data-action]").bind( "click", function() {
      //Send request for deletion
   });
}

Next, you need to bind this functionality to the click event of a link, which can be achieved as shown:

$(".remove-item").bind( "click", function() {
   show_dialog_for($this.prop("data-item"));
});

For example, with the following link containing the class property "remove-item" and data-item value of XXX:

<a href="www.mysite.com/product/1" class="remove-item" data-item="XXX" data-target="#myModal"  data-toggle="modal"><i class="icon-remove"></i> Delete</a>

This approach should assist you in achieving the desired functionality.

Answer №3

Check Out the Live Demo

To integrate this functionality into your website, make sure to include the following JavaScript code:

    $(".delBtn").click(function() {
        $("#delConfirmBtn").attr("href", $(this).attr("href"));
       $("#delConfirm").modal('toggle');
        return false;
    });

Here is the link that triggers the action:

<a href="del_products.html?idproduct=1" class="delBtn btn btn-default">Delete</a>

This link opens up the confirmation modal window:

<a href="#" id="delConfirmBtn" class="btn btn-danger">Delete</a>

The modal window structure looks like this:

  <!-- Modal -->
  <div class="modal fade" id="delConfirm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
          <h4 class="modal-title">Delete product</h4>
        </div>
        <div class="modal-body">
            <p>Lorem ipsum.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <a href="#" id="delConfirmBtn" class="btn btn-danger"><i class="icon-trash"></i> Delete</a>
        </div>
      </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
  </div><!-- /.modal -->

You can view and test the code on our custom demo site

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 is the appropriate utilization of the await keyword within concise if-else statements in JavaScript?

Along with transitioning away from jQuery, my main focus is to make my code more efficient. In large scale enterprise applications, the excessive use of jQuery and JavaScript can become problematic. I have decided to switch back to vanilla JavaScript for ...

"Utilizing the React library to implement an HTML component with a styled radio input that

My radio button issue: I have two radio buttons, one is checked by default. When trying to switch the selection by clicking on the unchecked option, I have to click twice. How can I resolve this problem? <label>YES</label> <input type={&a ...

Having trouble displaying Google Map using GMapPanel in EXTJS 4?

Ext.Loader.setPath('Ext.ux', '../ux'); Ext.require([ 'Ext.tree.*', 'Ext.data.*', 'Ext.window.MessageBox', 'Ext.window.*', 'Ext.ux.GMapPanel' ]); var map = new Ext.ux ...

The comparison between local variables and data can result in a significant drop in performance

My current project involves VueJS and Cesium, but I'm facing a performance issue with a significant drop in frame rate. While I have identified the problem area, I am unsure of why this is happening and how to resolve it. export default { name: ...

Is there a way to automatically update the URL to include $_GET['data'] (media.php?data=123) when a selection is made from a dropdown list?

I'm currently working on a project that involves a website in PHP and a MySQL database. I have successfully linked the two together, and now I have a combobox filled with data from the database. Below is my script for handling the selection: <scr ...

The canvas grid is malfunctioning and failing to display correctly

My current code allows me to draw a grid on a canvas with multiple lines, but I'm encountering an issue when trying to render 25 or more lines - some of them don't appear on the canvas. This problem becomes more noticeable when there are 40 or mo ...

Troubleshooting a cross-origin resource sharing problem in Express.js

I've set up Express as the backend for my client application, but I keep encountering an issue when trying to make a request to the GET /urls endpoint. The error message I receive is: Access to fetch at 'http://localhost:5000/urls' from orig ...

Issues arise when Angular properties become undefined following the initialization or OnInit functions

There seems to be a peculiar issue with the properties of an angular component that I have set up. It appears that these properties are losing their values after the initialization process. The component is essentially a basic HTML file containing a video ...

What is the reason for the result of 0x80000000 & 0x80000000 being lower than 0?

What is the reason for this inconsistency in nodejs? 0x80000000 & 0x80000000 < 0 while 0x40000000 & 0x40000000 > 0 Also, if I were to use a larger hexadecimal number like 0x800000000, could it potentially introduce hidden bugs? POSTAG.t ...

Creating a Slack-inspired interface with a Bootstrap 4 fixed bottom message box nested within a column

I'm attempting to create an interface similar to Slack, featuring a message box fixed at the bottom using Bootstrap 4. However, when I use the fixed-bottom class (where my custom message-box col is located), it spans the entire width of the window, wh ...

The content of my menu is concealed within a DIV. It may remain hidden or malfunction

I am in the process of creating master pages. In one of the master pages, I have implemented a dropdown menu using jQuery and CSS. While it works perfectly fine on some pages, it seems to be hidden on others that contain a div element. This is because the ...

"Implementing a bookmark/watch feature with a hover text cloud using either a Drupal module or CSS styling

Seeking guidance on implementing features in Drupal core. Interested in allowing users to subscribe to various aspects of a node, such as bookmarking for later viewing or rating it. Want a consistent theme across the site and for these features to use togg ...

The element type 'ReactElement<any>' does not match a JSX element constructor function. 'undefined' cannot be assigned to type 'Element | null'

After attempting the suggested fix of deleting node_modules/ and yarn.lock, then reinstalling everything, I still cannot resolve the issue. I am currently developing a basic router that renders children based on a certain prop: import React, { Fragment } ...

Steps for revealing all the information from a database when clicking on the 'Read More' button

I recently set up a database called Magazine, featuring a table named social_media. This table consists of 5 columns: ID, Headline, Author, Content Short, Content. The Content Short column contains condensed versions of the articles which are displayed on ...

Converting Latin numbers to Arabic using PHP

Looking for a way to convert English numbers to Arabic within specific elements in an HTML page using PHP? Here's my attempt at some code, but it ends up converting all numbers in the page. I only want to convert numbers between certain elements. Any ...

Looking for a simple method to generate a text-only dropdown link in Bootstrap 4 without using a button?

Is there a simple method to create a text-only dropdown link in Bootstrap 4 without using a button? Or do I have to adjust the padding and buttons using CSS to make the "Delivery" dropdown blend in with the rest of the text? I've searched online and ...

The React Router Dom V6 triggers loaders as soon as the router is initialized

I'm currently working on implementing routing into my application using react-router-dom V6, specifically utilizing the new createBrowserRouter function and RouterProvider component. The issue I'm facing is that when attempting to integrate a lo ...

Unexpected token N error was thrown while using the Jquery .ajax() function

While working on a form submission feature using JQuery .ajax() to save data into a MySQL database, I encountered an error message that says "SyntaxError: Unexpected token N". Can someone please explain what this means and provide guidance on how to fix it ...

Filter Observable based on object array property

I am trying to filter an Observable and only keep the stream that has a specific property value in an array of objects inside it. For example, consider this Observable: const observable = of({name: 'agency', year: '2010', job: [ ...

The functionality of jQuery's toggleClass is not functioning properly when clicking on a different thumbnail

My objective: I want to display a set of thumbnails. When a thumbnail is clicked, I want to toggle the hiddenElement class and show a large image inside a hidden div. If anywhere in the DOM is clicked, it should hide the current image or allow the user t ...