0

why this code not work? do this methods only work with css?

<input type="button" class="one" value="click Me" />
<input type="button" value="change Class" id="clk" />

<script>
$("#clk").click(function () {
    $('[value = "click Me"]').removeClass('one').addClass('tow');
    alert($('[value = "click Me"]').attr('class')); //print tow
});
$(".one").click(function () {
    alert('one');
});
$(".tow").click(function () {
    alert('tow');
});
</script>
Felix Kling
  • 756,363
  • 169
  • 1,062
  • 1,111
milad.b
  • 49
  • 1
  • 6

2 Answers2

1

Try Using event delegation As class tow doesn't exist when you are binding click event to it

$(document.body).on('click',".tow",function(){
      alert('tow');
});
Mohammad Adil
  • 44,013
  • 17
  • 87
  • 109
1
$('body').on('click', '.one', function (event) {
    alert('one');
});
$('body').on('click', '.tow', function (event) {
    alert('tow');
});
Tushar Gupta - curioustushar
  • 56,454
  • 22
  • 99
  • 107
Maxim Zhukov
  • 9,875
  • 5
  • 39
  • 82