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
  • 795,719
  • 175
  • 1,089
  • 1,143
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');
});
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
1
$('body').on('click', '.one', function (event) {
    alert('one');
});
$('body').on('click', '.tow', function (event) {
    alert('tow');
});
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
Maxim Zhukov
  • 10,060
  • 5
  • 44
  • 88