fanfavorite
fanfavorite

Reputation: 5199

Stop link behaviour in PrototypeJS, if passed condition, have link work as normal

I am using PrototypeJS's e.stop() function in a click observer, which I do some checks and if passes some conditions, I want the link to behave as normal, but stop all other elements from firing.

For example:

<script type="text/javascript"> 
document.observe('dom:loaded',function(){
   if ($$('a') != undefined) {
      $$('a').invoke('observe','click', function(e) {    
         if (SOME CONDITION){
            //NORMAL LINK FUNCTIONALITY
            //STOP PARENT
    } else {
            e.stop();
            //CALL FUNCTION
         }
      });
   }
   if ($$('.parent') != undefined) {
      $$('.parent').invoke('observe','click', function(e) {    
         alert('test');
      });
   }
});
</script> 
<div class="parent">
   <a href="javascript:popUp('test.html')">Test 1</a>
   <a href="test.html">Test 2</a>
   <a href="test.html" target="_blank">Test 3</a>
</div>

What happens is parent fires on click of any links. The e.stop() stops that, but stops normal link behaviour as well. Is there a way to tell the link to process as normal, but stop all others?

Upvotes: 0

Views: 138

Answers (1)

dievardump
dievardump

Reputation: 2503

When you catch events, you usually just have to "return true;" to have the normal behaviour.

Upvotes: 1

Related Questions