2

Here's what I tried so far:

var preventDefaultFunc = function(e){   
            e.preventDefault();
        }

then I call it in the click function later:

$('.item').click(function(e){

            var $this = $(this);
            var $thisChild = $this.find('.item-content');
            if ( $this.hasClass('big') ) {
                return false;
            }

            $this.on(preventDefaultFunc);
    // make the box bigger and load the article inside it here
});

I did it this way so I can turn it off again later, because the div in question needs to allow clicks within it once it's made big.

The on/off switch isn't working, since it's supposed to stop the page from loading the link within the box, so that I can make the box bigger and load the article with ajax instead. Note that on/off is the jquery 1.7 version of bind/unbind.

Any suggestions?

Graham Morley
  • 450
  • 1
  • 9
  • 20
  • 1
    Yes, the correct way t do the on event is (correct if wrong) $(..).on('click', function(e){..}) or $(..).on('click','a', function(e){..}). then to turn the click off its $(..).off('click') this will unbind the click, the preventDefault simple stops it from executing the href link hope this helps a little – Simon Davies Feb 23 '12 at 22:14

3 Answers3

5

trying to understand your meaning lets have astab at it :-)

  $('.item').on('click',function(e){
        e.preventDefault();
        var $this = $(this);
        var $thisChild = $this.find('.item-content');
         if ( $this.hasClass('big') ) {
             return false;
          }
       //-- this should unbind your click 
      $('.item').off('click');

      // make the box bigger and load the article inside it here
  });
Simon Davies
  • 3,668
  • 9
  • 41
  • 69
1

If you're looking to stop the default click action, just replace this:

$this.on(preventDefaultFunc);

With this:

e.preventDefault();

I'm not 100% clear on what you're trying to do, besides preventing the default click action, but it looks to me like you're overthinking it. If this doesn't help, please try explaining your setup and goals a little further.

Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61
1

Shouldn't it be more like $this.on('click', preventDefaultFunc) ?

raina77ow
  • 103,633
  • 15
  • 192
  • 229