1

I have this working code but why the checkbox is not affecting it?.

$("#btn").click(function() {
  if ($("#txt2").val() == 2) {
    alert("Hello");
    $("#txt").removeClass('validation');
    $("#chk").removeClass('validation');
  } else {
    $("#txt").addClass('validation');
    $("#chk").addClass('validation');
  }
});
.validation {
  border: 1px solid #ff0000;
  background-color: #ffeeee;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tr>
  <td>
    <input type="checkbox" id="chk">
    <input type="text" id="txt">
  </td>
</tr>
<input type="text" id="txt2">
<input type="button" id="btn" value="Click">

Anyone know about this issue or problem?.

Rajesh
  • 24,354
  • 5
  • 48
  • 79

3 Answers3

2

Your checkbox is getting the class (you can tell in the DOM inspector); you don't see the styling because the checkbox border isn't shown.

Some of the answers to this question suggests using outline rather than border for checkboxes; see the new style rule in the example below.

$("#btn").click(function() {
  if ($("#txt2").val() == 2) {
    alert("Hello");
    $("#txt").removeClass('validation');
    $("#chk").removeClass('validation');
  } else {
    $("#txt").addClass('validation');
    $("#chk").addClass('validation');
  }
});
.validation {
  border: 1px solid #ff0000;
  background-color: #ffeeee;
}
input[type=checkbox].validation {
  outline: 1px solid #ff0000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tr>
  <td>
    <input type="checkbox" id="chk">
    <input type="text" id="txt">
  </td>
</tr>
<input type="text" id="txt2">
<input type="button" id="btn" value="Click">
Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

$("#btn").click(function() {
  if ($("#txt2").val() == 2) {
    alert("Hello");
    $("#txt").removeClass('validation');
    $("#chk").removeClass('border');
  } else {
    $("#txt").addClass('validation');
    $("#chk").addClass('border');
  }
});
.validation {
  border: 1px solid #ff0000;
  background-color: #ffeeee;
}
.border {
  outline: 1px solid #ff0000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tr>
  <td>
    <input type="checkbox" id="chk">
    <input type="text" id="txt">
  </td>
</tr>
<input type="text" id="txt2">
<input type="button" id="btn" value="Click">
  1. Use outline
guradio
  • 15,524
  • 4
  • 36
  • 57
1

Acutally your checkbox is getting the class. Use your browser inspector to see it. The background-color alone has not effect on the checkbox.

To show you I change the cursor. Put your mouse on the checkbox then click on the button and put your mouse on it again.

$("#btn").click(function() {
  if ($("#txt2").val() == 2) {
    alert("Hello");
    $("#txt").removeClass('validation');
    $("#chk").removeClass('validation');
  } else {
    $("#txt").addClass('validation');
    $("#chk").addClass('validation');
  }
});
.validation {
  border: 1px solid #ff0000;
  background-color: #ffeeee;
  cursor: wait;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tr>
  <td>
    <input type="checkbox" id="chk">
    <input type="text" id="txt">
  </td>
</tr>
<input type="text" id="txt2">
<input type="button" id="btn" value="Click">
Weedoze
  • 13,683
  • 1
  • 33
  • 63