3

I have JQuery code:

$("#step1Skip").on("click", function(event){
    $('#chooseTheme, #addImages, #addVideo, #finalStep').hide();
    $('#addLogo').show();
});

If I use a button e.g.

<button type="button" id="step1Skip">Skip this step >></button>

This works fine. BUT, if I try the same with a simple link:

<a href="#" id="step1Skip">Skip this step</a>

It does nothing, simply doesn't work. Why is that?

I don't try them at the same time, so it's not an ID issue.

StudioTime
  • 22,603
  • 38
  • 120
  • 207

2 Answers2

8

Your browser is trying to follow the link. Try using .preventDefault()

$("#step1Skip").on("click", function(event){
    event.preventDefault();
    $('#chooseTheme, #addImages, #addVideo, #finalStep').hide();
    $('#addLogo').show();
});

This will prevent the default action of the link from occurring.

pmandell
  • 4,288
  • 17
  • 21
3

Since it is a link, the link is performing it's default action. You need to prevent it from doing so, and use preventDefault()

$("#step1Skip").on("click", function(event){
    event.preventDefault();
    $('#chooseTheme, #addImages, #addVideo, #finalStep').hide();
    $('#addLogo').show();
});

Another note - this is useful for only the <a>. Since buttons do not have a default action, they are unhindered by using event.preventDefault()

Take a look at http://api.jquery.com/event.preventDefault/

This question can be helpful on understanding how default actions work with a link.

Community
  • 1
  • 1
ddavison
  • 28,221
  • 15
  • 85
  • 110