views:

83

answers:

2

Hi,

I need to display the message with first time the button is pressed only. If I press the button again, it will redirect me. How to do in javascript?

I have submit button, first time press button it will show the message. Pressing the same button the next time should NEVER SHOW the message, it should forward to the next page.

Please show me how to do this in javascript?

+6  A: 

Use Events/one to bind a click event handler which fires just once. Within that event handler, bind a normal click event handler which performs your redirect, thus guaranteeing that the message is only displayed once:

$('#myButton').one("click", function() {
    alert('show me the first time only');
    $(this).click(function() {
       //or $('form').submit();
       //or 'return true'; depending on your specific requirement
       window.location.href = 'some/link.html';
    });
    return false;
});

The first time the user clicks, he will get the message. Subsequent clicks will forward him to the some/link.html without first showing the message.

karim79
A: 

Just unbind it on first invocation:

$('#someButton').click(function() {
  $(this).unbind('click', arguments.callee);
  alert('I only appear once');
});
kangax