0

I have the following code:

  if(amStatus === "Logout") {
    $('#row-qfauto-1 .large-9').append('<a id="next-password" class="button button-next" href="javascript:;">Next</a>')
    $('#next-password').click(function() {
      $('#row-html2-0').addClass('next-visible')
      $(window).resize()
    })
  }

  $('.button-next').on('click', function() {
    $(this).hide()
    $(window).resize()
  })

As you can see button-next is being added dynamically with jQuery. So in order to make the button disappear when clicked I decided to use on.

However, the button doesn't disappear. It just stays there. When I do

  $('.button-next').click(function() {
    $(this).hide()
    $(window).resize()   
  })

twice in the console, then the button disappears. Weird. What could be the problem?

Live site: http://www.chineselearnonline.com/amember/signup/cloprogressive

alexchenco
  • 53,565
  • 76
  • 241
  • 413

2 Answers2

1

Try this:

if (amStatus === "Logout") {
  $('#row-qfauto-1 .large-9').append('<a id="next-password" class="button button-next" href="javascript:;">Next</a>')
  $('#next-password').click(function() {
    $('#row-html2-0').addClass('next-visible')
    $(window).resize()
  })
}

$('#row-qfauto-1 .large-9').on('click', '.button-next', function(e) {
  e.preventDefault();
  $(this).hide()
  $(window).resize()
});

Note: You are assigning event listener to #next-password as well as .button-next, is this the way you want to implement ?

Also make sure you dont have redundant id in your DOM

Rayon
  • 36,219
  • 4
  • 49
  • 76
0

Try event delegation

$(document).on("click", ".button-next", function() {
  // do stuff
})

or attaching click event when element created using jQuery()

   $("#row-qfauto-1 .large-9")
   .append($("<a></a>", {
       "id":"next-password",
       "class":"button button-next",
       "href":"javascript:;"
       "on": {
            "click": function() {
                       $(this).hide()
                       $(window).resize()
                     }
       },
       "html":"Next"  
    }))
guest271314
  • 1
  • 15
  • 104
  • 177