1

I updated my code and rephrased my question:

I am trying to create the following condition. When a link with an empty href attribute (for example href="") is clicked, a modal is launched and the default behavior of that link is prevented..

But when the href attribute contains a value (href="www.something.com") I would like for the link to work as it normally does using its default behavior.

For some reason my code isn't working. Any help is appreciated.

      // Semicolon (;) to ensure closing of earlier scripting
      // Encapsulation
      // $ is assigned to jQuery
      ;(function($) {
           // DOM Ready
          $(function() {

              // Binding a click event
              // From jQuery v.1.7.0 use .on() instead of .bind()
              $('.launch').bind('click', function(e) {
                var attrId = $(this).attr('attrId');

                if( $('.launch').attr('href') == '') {

                    // Prevents the default action to be triggered. 
                    e.preventDefault();

                    // Triggering bPopup when click event is fired
                    $('div[attrId="' + attrId+'"]').bPopup({
                      //position: ['auto', 'auto'], //x, y
                      appendTo: 'body'
                    });
                }  else {
                  $(this).removeAttribute('attrId');
                  return true;
                }  

              });

          });

      })(jQuery);
Spanky
  • 699
  • 2
  • 11
  • 38

2 Answers2

2

Your jQuery is... very wrong. $('href').attr('') is getting the empty attribute from an element <href>... Did you mean:

if( $(this).attr('href') == '')
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

A few things: event is undefined, as your event object is simply called e. Secondly, e.stopPropagation(); will not do what you want. stopPropagation simply prevents parent events from firing (eg: clicking a <td> will also fire click on the containing <tr> unless stopped).

Try just replacing your else statement with

return true;

Also your jQuery is incorrect (as stated in the other answer here).

This answer may help as well:

How to trigger an event after using event.preventDefault()

Good luck!

Community
  • 1
  • 1
CollinD
  • 7,304
  • 2
  • 22
  • 45
  • Thanks for your detailed explanation. I see that I used event instead of e. Thanks for that – Spanky Sep 10 '15 at 19:46