Neriwka
Neriwka

Reputation: 3

Jquery enable/disable button textarea validate

Jsfiddle example

As an example, I want to do the operation. It doesn't work alone. I threw it in my server, it didn't work when I run it Where is the Error?

$( "#validateTxt" ).change(function() {
 if($( "#validateTxt" ).val()=="123"){
 $('#btnCheck').attr("enabled",false);
 }
 else{
  $('#btnCheck').attr("disabled",true);
 }
 
});
<!DOCTYPE html>
<html lang="en" >
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  
  <title></title>
  

</head>
<body>
<!-- partial:index.partial.html -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" integrity="sha384-3ceskX3iaEnIogmQchP8opvBy3Mi7Ce34nWjpBIwVTHfGYWQS9jwHDVRnpKKHJg7" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.3.7/js/tether.min.js" integrity="sha384-XTs3FgkjiBgo8qjEjBk0tGmf3wPrWtA6coPfQDfFEY8AnYJwjalXCiosYRBIBZX8" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js" integrity="sha384-BLiI7JTZm+JWlgKa0M0kGRpJbF2J8q+qreVrKBC47e3K6BW78kGLrCkeRX6I9RoK" crossorigin="anonymous"></script>

////üst kodlar boostrap kütüphanesini çağırıdıgım kodlar projende yoksa ekle 

<input type="text" id="validateTxt" class="form-control" placeholder="123">


<input type="button" class="btn btn-success" value="KontrolEt" id="btnCheck" disabled>
<!-- partial -->
  <script  src="./script.js"></script>
 

</body>
</html>

Upvotes: 0

Views: 355

Answers (2)

Mamun
Mamun

Reputation: 68933

There is no enabled attribute for the <input> element.

You can set the disabled to false if the condition is true else remove the attribute using .removeAttr().

Also I will prefer using the input event instead of change:

$("#validateTxt").on('input', function() {
 if($("#validateTxt").val()=="123"){
   $('#btnCheck').attr("disabled", true);
 }
 else{
   $('#btnCheck').removeAttr("disabled");
 }
});
<!-- partial:index.partial.html -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" integrity="sha384-3ceskX3iaEnIogmQchP8opvBy3Mi7Ce34nWjpBIwVTHfGYWQS9jwHDVRnpKKHJg7" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.3.7/js/tether.min.js" integrity="sha384-XTs3FgkjiBgo8qjEjBk0tGmf3wPrWtA6coPfQDfFEY8AnYJwjalXCiosYRBIBZX8" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js" integrity="sha384-BLiI7JTZm+JWlgKa0M0kGRpJbF2J8q+qreVrKBC47e3K6BW78kGLrCkeRX6I9RoK" crossorigin="anonymous"></script>


<input type="text" id="validateTxt" class="form-control" placeholder="123">

<input type="button" class="btn btn-success" value="KontrolEt" id="btnCheck" disabled>

Upvotes: 1

prasanth
prasanth

Reputation: 22490

You could do like this

$('#btnCheck').attr("disabled", false);

Html don't have enabled attribute .So you need to set false for disabled attribute

$("#validateTxt").change(function() {
  if ($("#validateTxt").val() == "123") {
    $('#btnCheck').attr("disabled", false);
  } else {
    $('#btnCheck').attr("disabled", true);
  }

});
<!DOCTYPE html>
<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  <title></title>


</head>

<body>
  <!-- partial:index.partial.html -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" integrity="sha384-3ceskX3iaEnIogmQchP8opvBy3Mi7Ce34nWjpBIwVTHfGYWQS9jwHDVRnpKKHJg7" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.3.7/js/tether.min.js" integrity="sha384-XTs3FgkjiBgo8qjEjBk0tGmf3wPrWtA6coPfQDfFEY8AnYJwjalXCiosYRBIBZX8" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js" integrity="sha384-BLiI7JTZm+JWlgKa0M0kGRpJbF2J8q+qreVrKBC47e3K6BW78kGLrCkeRX6I9RoK" crossorigin="anonymous"></script>

  ////üst kodlar boostrap kütüphanesini çağırıdıgım kodlar projende yoksa ekle

  <input type="text" id="validateTxt" class="form-control" placeholder="123">


  <input type="button" class="btn btn-success" value="KontrolEt" id="btnCheck" disabled>
  <!-- partial -->
  <script src="./script.js"></script>


</body>

</html>

Upvotes: 1

Related Questions