user2516546
user2516546

Reputation: 111

JQuery on click function, except one?

I'm using javascript to display a notification when a user clicks on an tag (Are you sure you want to leave this page?) but I don't want it to work on the one that posts the form. Is there any way of making it work for all tags except one?

$(document).ready(function()
{
    $('a').on('click', function()
    {
        var note=$("#formnote").val();
        if (note.length > 0){
            var redirectquestion = confirm("Are you sure that you want to leave this page?");
            if(!redirectquestion ){
                return false;
            }
        }
    });
});

Upvotes: 0

Views: 101

Answers (5)

Avin Varghese
Avin Varghese

Reputation: 4370

$(document).ready(function () {
    $("a:not(#link_submit)").on('click', function () {
        var note = $("#formnote").val();
        if (note.length > 0) {
            var redirectquestion = confirm("Are you sure that you want to leave this page?");
            if (!redirectquestion) {
                return false;
            }
        }
    });
});

Upvotes: 1

sdd
sdd

Reputation: 11

This should work also - if your a-tag has the id 'submit'.

$(document).ready(function () {
    $('a').not('#submit').on('click', function () {
        var note = $("#formnote").val();
        if (note.length > 0) {
            var redirectquestion = confirm("Are you sure that you want to leave this page?");
            if (!redirectquestion) {
                return false;
            }
        }
    });
});

Upvotes: 0

Eric Martin
Eric Martin

Reputation: 2405

With not selector :

$(document).ready(function () {
$('a:not(#link_submit)').on('click', function () {
    var note = $("#formnote").val();
        if (note.length > 0) {
            var redirectquestion = confirm("Are you sure that you want to leave this page?");
            if (!redirectquestion) {
                return false;
            }
        }
    });
});

With .not() :

$(document).ready(function () {
$('a').not('#link_submit').on('click', function () {
    var note = $("#formnote").val();
        if (note.length > 0) {
            var redirectquestion = confirm("Are you sure that you want to leave this page?");
            if (!redirectquestion) {
                return false;
            }
        }
    });
});

Have a nice day.

Upvotes: 0

George Yates
George Yates

Reputation: 1247

I would use jQuery's .not() so that you're not binding an event handler to that element and using resources to not do anything. Also, you can easily assign a click handler to that element later without worrying about your previous code. So something like this:

$('a').not('.submitLink').on('click', function() {
    ...
});

Upvotes: 0

Gordon Tucker
Gordon Tucker

Reputation: 6773

Something like this should work if I understand te question right (and assuming you add the class submitLink to the tag you don't want to trigger this call from

$(document).ready(function()
{
  $('a:not(.submitLink)').on('click', function()
  {
    var note=$("#formnote").val();
    if (note.length > 0){
      var redirectquestion = confirm("Are you sure that you want to leave this page?");
      if(!redirectquestion ){
        return false;
      }
    }
  });
});

Upvotes: 0

Related Questions