Stacker
Stacker

Reputation: 113

How can I get a function to check if a property is disabled?

I am trying to get my function to check if a field is disabled, and if so put a border around it. I have the following code: HTML

<br />

<input id="input1" disabled/>

<button onclick="myFunction()">
Button
</button>

Script:

   function myFunction() {
         var confirmPassword = $("#input1").val();
          if (confirmPassword.prop('disabled')){
          alert("disabled");
          } else {
                        alert("enabled");
         }
     }

FIDDLE: https://jsfiddle.net/4vn5kemo/

Upvotes: 1

Views: 44

Answers (1)

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28455

You need to update from

var confirmPassword = $("#input1").val();

to

var confirmPassword = $("#input1");

For reference - http://plnkr.co/edit/JXN2ZU0PRNq7vgLvDeSl

 function myFunction() {
         var confirmPassword = $("#input1");
          if (confirmPassword.prop('disabled')){
          alert("disabled");
          } else {
                        alert("enabled");
         }
     }
/* Put your css in here */

h1 {
  color: red;
}
<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8" />
    <title></title>
    <link rel="stylesheet" href="style.css" />
    <script data-require="jquery" data-semver="2.2.0" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script src="script.js"></script>
  </head>

  <body>
   <input id="input1" disabled/>

<button onclick="myFunction()">
Button
</button>
  </body>

</html>

Upvotes: 1

Related Questions