Disable / Enable button when the text and textarea are empty
Asked Answered
S

8

5

I have this code that disable the button when the text is empty, but I have a textarea html code. How can I include this that when the text and textarea are both empty the button will be disabled and when both are filled it enables. I tried the code below and it works on text only. Any ideas?

$(document).ready(function() {
    $('input[type="submit"]').attr('disabled', true);
    
    $('input[type="text"]').on('keyup',function() {
        if($(this).val() != '') {
            $('input[type="submit"]').attr('disabled', false);
        } else {
            $('input[type="submit"]').attr('disabled', true);
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" name="textField" />
<textarea rows="4" cols="30" ></textarea>
<input type="submit" value="next" />
Second answered 6/11, 2013 at 7:15 Comment(2)
you have missed the textarea selector in jquery.... so it's not workin.....?Velez
That's my problem I need to check also the textarea if it is fill in before the button gets enabled.Second
T
9

You miss the textarea selector in jQuery

$(document).ready(function() {
    $('input[type="submit"]').attr('disabled', true);
    
    $('input[type="text"],textarea').on('keyup',function() {
        var textarea_value = $("#texta").val();
        var text_value = $('input[name="textField"]').val();
        
        if(textarea_value != '' && text_value != '') {
            $('input[type="submit"]').attr('disabled', false);
        } else {
            $('input[type="submit"]').attr('disabled', true);
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" name="textField" /><br>
<textarea rows="4" cols="30" id="texta"></textarea><br>
<input type="submit" value="next" />
Tropaeolin answered 6/11, 2013 at 7:27 Comment(0)
K
3

You can do this using the .prop() method like:

// Cache the elements first
var $text = $('input[type="text"]');
var $textarea = $('textarea');
var $submit = $('input[type="submit"]');

// Set the onkeyup events
$submit.prop('disabled', true);
$text.on('keyup', checkStatus);
$textarea.on('keyup', checkStatus);

// Set the event handler
function checkStatus() {
    var status = ($.trim($text.val()) === '' || $.trim($textarea.val()) === '');
    $submit.prop('disabled', status);
}

F.Y.I.

As mentioned in the .prop() API Documentation:

Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.


FIDDLE DEMO

Kovrov answered 6/11, 2013 at 7:28 Comment(0)
K
0

use prop

$('input[type="submit"]').prop('disabled', true);
$('input[type="text"],textarea ').on('keyup',function() {
    if($(this).val()) {
        $('input[type="submit"]').prop('disabled' , false);
    }else{
        $('input[type="submit"]').prop('disabled' , true);
    }
});

by attr we can do by

 $('input[type="submit"]').attr('disabled', 'disabled');
    $('input[type="text"],textarea ').on('keyup',function() {
        if($(this).val()) {
            $('input[type="submit"]').removeAttr('disabled');
        }else{
            $('input[type="submit"]').attr('disabled','disabled');
        }
    });

see .prop() vs .attr()

Kilian answered 6/11, 2013 at 7:16 Comment(1)
It doesn't work. I want that when both the text and textarea are filled in that's the time the button will be enabled.Second
S
0

Just check both input feild and textarea. For that you can bind the both fields to keyup event and check the value

$('input[type="submit"]').prop('disabled', true);
$("#yourtextfield,#yourtextarea").on("keyup","#parentdiv",function(){
if($("#yourtextfield").val() == '' || $("#yourtextarea").val() == ''){
   $('input[type="submit"]').prop('disabled' , true);
   }
else{
   $('input[type="submit"]').prop('disabled' , false);
   }
})
Safranine answered 6/11, 2013 at 7:23 Comment(0)
F
0

I think you have to check space.

$("textarea").on('mouseout', function(){
  if (!$.trim($("textarea").val())) {
    alert("empty");
 }
});

test it : http://jsfiddle.net/mehmetakifalp/ef5T9/

Footpound answered 6/11, 2013 at 7:28 Comment(0)
H
0
<input type="text" name="textField" />
<textarea rows="4" cols="30" id="texta"></textarea>
<input type="submit" value="next" />

<script type="text/javascript">
    $(document).ready(function() {
        $('input[type="submit"]').attr('disabled', true);
        $('input[type="text"],textarea').on('keyup',function() {
        var textarea_value = $("#texta").val();
        var text_value = $('input[name="textField"]').val();

        if(textarea_value != '' && text_value != '') {
            $('input[type="submit"]').attr('disabled' , false);
        }else{
            $('input[type="submit"]').attr('disabled' , true);
        }
        });

    });
</script>
Huron answered 14/2, 2018 at 6:39 Comment(0)
F
0

The selected answer does the job but it is not enough. A text area and text field filled with spaces ( pressing the space bar several times) will enable the submit button.

You therefore need to apply $.trim() to the values from these fields before passing them to the if statement as shown below

$(document).ready(function() {
$('input[type="submit"]').attr('disabled', true);

$('input[type="text"],textarea').on('keyup',function() {
    var textarea_value = $.trim($("#texta").val());
    var text_value = $.trim($('input[name="textField"]').val());

    if(textarea_value != '' && text_value != '') {
        $('input[type="submit"]').attr('disabled', false);
    } else {
        $('input[type="submit"]').attr('disabled', true);
    }
});
});
Featheredge answered 10/9, 2018 at 22:18 Comment(0)
L
0

I needed a solution where there are 2 text fields and a Submit button. The business logic was that the user should type in a value in any one of the text fields at a minimum to enable the Submit button.

Here is my solution which I used in my code. It is not the best/optimal solution possibly but it did the job. Do comment if you have a better way.

//Enable Submit button for search only on text input

$(".inputFieldCSSClass").on('keyup', function(){
     var isEmpty = !($.trim($("#inputText1").val()).length > 0 || 
                     $.trim($("#inputText2").val()).length > 0);
      $("#btnSubmit").prop('disabled', isEmpty);
 }); 
Lionfish answered 24/12, 2019 at 4:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.