Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8404921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:43:22+00:00 2026-06-09T22:43:22+00:00

I’m asking this question only for learning purpose as I solved the problem even

  • 0

I’m asking this question only for learning purpose as I solved the problem even before writing this, but maybe you’ll point out something useful for someone else.

I’m using Twitter Bootstrap and its modal lib and here’s what my code does. We open a modal, ask the user to input some json and then, the user press a “compute button” to compute the data and then the modal closes because of the data-dismiss.

I just want to know if it’s possible to prevent an action triggered by the html data-dismiss attribute using jQuery, javascript or something else.

Here is the HMTL code for the modal:

<div class="modal hide fade" id="source_modal">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">Γ—</button>
        <h3>Manually insert the data</h3>
    </div>
    <div class="modal-body">
        <textarea class="source_input">Insert your data here</textarea>
        <div class="clear"></div>
        <a href="#" class="btn action_clear">Clear</a>
    </div>
    <div class="modal-footer">
        <a href="#" class="btn" data-dismiss="modal">Close</a>
        <a class="btn btn-primary action_compute" data-dismiss="modal" href="#"><i class="icon-fire"></i> Compute</a>
    </div>
</div>

Note the last button (compute button) has data-dismiss="modal"

So basically, there’s a try catch function attached to the action_compute class of the anchor.

See this jQuery code:

// compute graph with json input
  $('.action_compute').on('click', function(event) {
    try
    {
      var json = JSON.parse($('.source_input').val());
      create_graph(json);
    }
    catch(err)
    {
      txt="There was an with your json data.\n";
      txt+="Error description: " + err.message + "\n\n";
      txt+="you can try to validate it with JSONLint. You may also leave the textarea blank for default behaviour";
      alert(txt);
    }
  });

Is it possible to prevent the action triggered by data-dismiss?

Note:

  • The Twitter bootstrap provide a jquery method to close the modal so I removed data-dismiss and I dismiss the modal using $('#upload_modal').modal('hide'); only if I get to the end of the try so it does what I want.
  • The jQuery event.preventDefault(); doesn’t stop the dismiss action when placed on the first line of the catch(err) jQuery code πŸ˜‰
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:43:22+00:00Added an answer on June 9, 2026 at 10:43 pm

    Try adding a listener to prevent the execution of the hide method.

    $('.modal').one('hide', function (e) {e.preventDefault()});​​​​​​​​​​​​​​​
    

    Place this inside the error catching section. As long as the click event is handled before the call to hide(), this should work.

    JSFiddle


    Since you did say the question was for learning purposes, I’ll add some additional info here on the architecture of Bootstrap plugin events.

    Many of the Bootstrap plugins include both “before” and “after” events. The “before” events, in this case hide, are triggered and propagate up the DOM before any of the essential actions of the associated methods have been executed. When execution is returned back to the originally triggering methods, the first thing that happens is a check to see if the action has been cancelled:

    if (e.isDefaultPrevented()) return
    

    Hence, any event listener which calls e.preventDefault() will effectively cancel the action. Simple and as you would expect.

    Any well implemented jQuery plugin which triggers events should have this functionality.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.