I have 2 buttons, click one will swap classes for both buttons.
The classes are selected
and unselected
.
My CodePen:
http://codepen.io/leongaban/pen/iLBvs
For example: click the Login button will remove it's unselected class and give it its selected class and give the Register button its unselected class.
Now the problem is that the register_unselected
class doesn't work. The unselected
classes are the ones that have click actions in my jQuery.
I should be able to keep clicking the gray button and turning it blue. However after clicking the first gray button (login) the new gray button register doesn't work.
Using Chrome's inspector I can clearly see that my register_unselected class has been added, yet the click function doesn't work.
Any ideas? How would you approach this?
// Login Tab (default : first)
$(".login_unselected").click(function(){
console.log('clicked login');
// Show Login Tab as selected
$(this)
.removeClass('login_unselected')
.addClass('login_selected');
// Show Register Tab as unselected
$(this).parent().find('#tab_register')
.removeClass('register_selected')
.addClass('register_unselected');
});
// Register Tab
$(".register_unselected").click(function(){
console.log('clicked register');
// Show Login Tab as selected
$(this)
.removeClass('register_unselected')
.addClass('register_selected');
// Show Register Tab as unselected
$(this).parent().find('#tab_login')
.removeClass('login_selected')
.addClass('login_unselected');
});
event delegation
, the click event doesn't look for elements that gain that class when it binds, just elements that exist with that class at page load. the quick fix is$(document).on('click', 'class', function(){ //normal code});
while replacingdocument
with a static element that does not change. – Assai