1

Based on the answer here, I tried to create a similar validation function:

<html>
    <head>
        <script>
            $("#my_name_id").on("change", function() {
                if (!$("#my_name_id").val()) {
                $("#button_id").attr("disabled", "disabled");
            } else {
                $("#button_id").attr("enabled", "enabled");
            }
                });
        </script>
    </head>
    <body>
        <form action="" name="test_form" id="test_form_id" method="post">
            <input type="text" name="my_name" id="my_name_id" placeholder="Type your name"/>
            <button type="submit" id="button_id">My button</button>
        </form>
    </body>
</html>

In this example, I would like to continually check to see if the input box contains any characters. If it does, then the button should be enabled (clickable). If the input box does not contain any text, then the button should be disabled.

However, in my case the button always remains enabled. What am I doing wrong here?

1
  • Can't you just do <input name="my_name" required />? Commented May 11, 2014 at 17:38

3 Answers 3

1

You should use prop, not attr. After all your code will become simpler:

$("#my_name_id").on("change keyup", function() {
    $("#button_id").prop("disabled", !this.value);
})
.trigger('change');

Also note how you can use trigger in order to run initial check on page load automatically. I also included keyup event for this to work as you type.

Demo: http://jsfiddle.net/7nywe/

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

7 Comments

Your example works if I type some text, then click outside the input box. However, I would like it so that as soon as you type something, the button becomes enabled (without having to click outside first).
Cool, that worked :) Could you please update your answer to include the keyup?
The jsfiddle you provided works, but when i create a .html file on my desktop and try it manually it doesn't work (the button remains enabled). Is there any additional code I need to include/enable?
Make sure you execute this code when DOM is loaded. Try to wrap it into document ready closure $(function() { /* code goes here */ }). Or move the code after the form tag.
When you say DOM is loaded, do you mean wrapping the entire solution in another $(function() { $("#my_name_id").... })? I did however place the <script>...</script> block after the form section, same results.
|
1

No enabled attribute in HTML for , so manipulate the disabled attribute:

<script>
$("#my_name_id").on("change", function () {
    if (!$("#my_name_id").val()) {
        $("#button_id").attr("disabled", "disabled");
    } else {
        $("#button_id").removeAttr("disabled");
    }
}).trigger('change');;
</script>

3 Comments

I tried your solution, however the button is still clickable upon an initial loading of the html.
@DirtyPenguin Well, set it as disabled by default.
If I set it as disabled by default, when I start typing in text in the input box, the button is not enabled.
0

You should use prop()

$("#button_id").prop("disabled", true);  // disabled
$("#button_id").attr("disabled", false); // enabled

You should use prop when you are dealing with boolean types.

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.