How to fire on click function fire only one time?

http://jsfiddle.net/btmg3u6f/

I mean when click on one or two that only first time scroll that id? In second click do nothing?

Thanks.

Just use a function called โ€œoff.โ€ Please see below.

$("a[href^='#']").click(function scroll(e) {
  e.preventDefault();

  var position = $($(this).attr("href")).offset().top;

  $("body, html").animate({
    scrollTop: position
  } /* speed */ );
  $(this).off('click')
});
1 Like

Thanks for answer!

I try it http://jsfiddle.net/L2yqw4ha/ but not seems work I mean when click on it agan and again do the event.

You have to choose the latest version of jQuery.

http://jsfiddle.net/pwbr08ef/

Is it fine now still my question how can do that like above vanilla JS example to do nothing about click? I mean when click on it do nothing? Because is not do animate but still does to jump to id.

Thanks!

There

$("a[href^='#']").click(function(e) {
  e.preventDefault();

  var position = $($(this).attr("href")).offset().top;

  $("body, html").animate({
    scrollTop: position
  } /* speed */ );
  $(this).off('click');
  $(this).click(function(e) {e.preventDefault()} )
});

1 Like

Here is a slightly different way to do it with jQuery:
http://jsfiddle.net/L2yqw4ha/

1 Like

@kerafyrm02 My links only work one time.

Yes you are right only one time and yes is about .one function. Thank you!

I thought I had clicked save. Try this one.

http://jsfiddle.net/1ykdh28p/

Why do you think it is better than your use of the one method?

1 Like

Ahah now whitch the best one to accept :smiley: ?