2

I need to find whether a HTML text box is disabled or not in IF{} Statement using jquery..

I tried like this...

  $('[id$=txtpieceleny]').attr("disabled",true);

  if($('[id$=txtpieceleny]').attr("disabled") == "true") {
     alert("Hello");
  }

Kindly help me...

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
Arun prasath
  • 53
  • 1
  • 8

3 Answers3

1

Try to use .is(":disabled") to identify that,

if ($('[id$=txtpieceleny]').is(":disabled")) {

But since you are invoking this over a collection of elements the result would be an outcome of OR operation. That means if any one of the text box is disabled in that collection, the result would be true. And FYKI, please don't use .attr() for setting properties use .prop() instead.

And here is the reason why we should not use .attr() in this case.

Community
  • 1
  • 1
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
0

you can compare directly

Use attr it will return string type

if($('[id$=txtpieceleny]').attr("disabled")) {
    alert("Hello");
}

Or

if ($('[id$=txtpieceleny]').attr("disabled") == "disabled") {
   alert("Hello");
}

Use Prop it will return true or false

if ($('[id$=txtpieceleny]').prop("disabled")) {
   alert("Hello");
}
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
Balachandran
  • 9,567
  • 1
  • 16
  • 26
0

Use :disabled selector in jquery to check

if ($('[id$=txtpieceleny]:disabled')){


}

or

if($('[id$=txtpieceleny]').attr("disabled")) {
     alert("Hello");
  }
Sudharsan S
  • 15,336
  • 3
  • 31
  • 49