1

I know it's easy to do using < button > or < input type="submit" but how would you keep this button disabled unless both input fields are filled?

<input id="one" type="text">
<input id="two" type="text">
<a href="#" class="button">OK</a>

4 Answers 4

2

Tie an event to both inputs, and check that both have values. Then enable the link.

$('#one, #two').blur(function() {
   if($('#one').val() !== "" && $('#two').val() !== "") {
       $('.button').attr('href','#');
   } else {
      $('.button').removeAttr('href');
   }
});

and change your html to:

<a class="button">OK</a>

so that the link is disabled on page load. Here's a JSFiddle demo.

Sign up to request clarification or add additional context in comments.

1 Comment

You nailed it! Thank you.
0

$(document).ready(function() {

  $inputs = $('#one,#tow');
  $inputs.change(check);
  $submit = $('#submit');

  function check() {
    var result = 1;
    for (var i = 0; i < $inputs.length; i++) {
      if (!$inputs[i].value) {
        result = 0;
        break;
      }
    }
    if (result) {
      $submit.removeAttr('disabled');
    } else {
      $submit.attr('disabled', 'disabled');
    }
  }

  check();
});

suggest use angular form

Comments

0

$(document).ready(function(){
       //$(".button").attr('disabled', "disabled");
        $(".button").click(function(){
           one = $("#one").val();
           two = $("#two").val();
           if(one && two){
             ///both fields filled. 
             return true;
           }
           //one or both of them is empty
                  
 
          return false;
       });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="one" type="text">
<input id="two" type="text">
<a href="#" class="button">OK</a>

1 Comment

Shouldn't it be two = $("two").val( )?
0

This is my implementation if facing this kind of situation. First, am add disabled class onto anchor tag on page load by using this style :

.disabled {
  color : gray // gray out button color
  cursor : default; // make cursor to arrow
  // you can do whatever styling you want
  // even disabled behaviour
}

We add those class using jquery on document ready together with keyup event like so :

$(function () {

   // add disabled class onto button class(anchor tag)
   $(".button").addClass('disabled');

   // register keyup handler on one and two element
   $("#one, #two").keyup(function () {

      var one = $("#one").val(),
          two = $("#two").val();
      // checking if both not empty, then remove class disabled
      if (one && two) $(".button").removeClass('disabled');
      // if not then add back disabled class
      else $(".button").addClass('disabled');

   });

   // when we pressing those button
   $('.button').click(function (e) {
     // we check if those button has disabled class yet
     // just return false
     if ($(this).hasClass('disabled')) return false;
   });
});

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.