5

This is my part of my html

<input type="text" name="age" />
<input type="text" name="poscode" />
<input type="submit" name="submit" value="Next >>" disabled="disabled"/>

This is my script

<script type="text/javascript">
$(function(){
var enable = false;
if($("input[name='age']").val != "")
enable = true;
if($("input[name='poscode']").val != "")
enable = true;
if(enable == true) $("input[name='submit']").attr('disabled', '');
});
</script>

This is not working, any idea what i'm doing wrong?

After user filled up two input age & poscode, the submit button should become active ( disabled at start)

1
  • OK, I see HTML and I see some jQuery, now where is the question? Commented Jul 22, 2010 at 12:50

6 Answers 6

11

You can do something like this:

$('input[name="age"], input[name="poscode"]').change(function(){
  if ($(this).val())
  {
    $("input[name='submit']").removeAttr('disabled');
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

@KennyTM: Agreed, I find myself more used to removeAttr in those cases :)
@Sarfraz: +1 I think it's more clear as to what it does as well.
4

To enable/disable when key is pressed do this way:

$('input[name="age"], input[name="poscode"]').keyup(function(){
  if ($(this).val())
{
  $("input[name='submit']").removeAttr('disabled');

}else{
$("input[name='submit']").attr('disabled','disabled');
}
});

2 Comments

This works but if only space is give the button gets enable
trim the input with trim() method. if ($(this).val().trim())
3

Give this a shot, here is a fiddle that I created so you can play with it.

http://jsfiddle.net/9sxwN/

Comments

0

Did you attach this to the input fields in any way? The way your code is written, I believe the function is called once, after the document has been loaded, and then simply discarded. Give your function a name and attach it to onblur or onchange.

1 Comment

hi, can you please tell me how, i am very new to jquery & js
0

Try this example code please

<div class="form-row align-items-center">
          <div class="col-auto">
              <input  type="text" name="inputComment" class="form-control" id="inputComment" placeholder="Your comment goes here">
          </div>
          <div class="col-auto">
               <button disabled id="btnSaveComment" type="button" class="btn btn-success">Save</button>
          </div>
</div>

<script>
  document.getElementById("btnSaveComment").disabled = true;

  $(document).ready(function () {
    $("#inputComment").on("change paste keyup", function () {
                var text = $(this).val();
                if (text.length > 0) {
                    document.getElementById("btnSaveComment").disabled = false;
                }
                else {
                    document.getElementById("btnSaveComment").disabled = true;
                }
            });
        });
</script>

Comments

-1

Like sAc already mentioned, you have to remove the attribute 'disabled' altogether.

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.