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
  • 705,106
  • 160
  • 1,004
  • 1,072
milad.b
  • 49
  • 5

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
  • 43,337
  • 17
  • 86
  • 109
1
$('body').on('click', '.one', function (event) {
    alert('one');
});
$('body').on('click', '.tow', function (event) {
    alert('tow');
});
Tushar Gupta - curioustushar
  • 54,013
  • 22
  • 95
  • 103
Maxim Zhukov
  • 9,627
  • 5
  • 37
  • 78