Arun prasath
Arun prasath

Reputation: 53

How to compare attribute of a HTML Text box in jquery?

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...

Upvotes: 2

Views: 106

Answers (3)

Balachandran
Balachandran

Reputation: 9637

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");
}

Upvotes: 0

Sudharsan S
Sudharsan S

Reputation: 15393

Use :disabled selector in jquery to check

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


}

or

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

Upvotes: 0

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

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.

Upvotes: 1

Related Questions