TerrorBladez
TerrorBladez

Reputation: 9

Jquery keyup function not working

In the $(':text').keyup(function() { I tried changing the :text into :number doesn't work only works if I remove the && $('#number').val() != ""

<script>
$(document).ready(function(event) {  
$(':text').keyup(function() {
    if($('#input').val() != "" && $('#number').val() != "" 

    ){
       $('#submit').removeAttr('disabled');
    } else {
       $('#submit').attr('disabled', true);   
    }
});});
</script>

HTML

<form><input type=text id='input'>
<input type=number id='number'><br>
<input type=button id='submit' value='submit'>
    </form>

jsfiddle: http://jsfiddle.net/LaLL0v6a/

Upvotes: 0

Views: 2662

Answers (3)

Mad Angle
Mad Angle

Reputation: 2330

Change $(':text') to $('input[type="text"]')

Upvotes: 2

Arun P Johny
Arun P Johny

Reputation: 388436

There are 2 problems.

  1. You are registering the keyup handler to only the first input element, not the second one because its type is number
  2. Use .prop() to set the disabled state
  3. If you enter a non numerical value in number field then it will remain disabled because .val() will return an empty string

so

$(document).ready(function (event) {
    $('#input, #number').keyup(function () {
        $('#submit').prop('disabled', $('#input').val() == '' || $('#number').val() == "");
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type=text id='input' />
<input type=number id='number' />
<input type=button id='submit' value='submit' disabled />

Upvotes: 2

coDe murDerer
coDe murDerer

Reputation: 1876

Use

$('#input').keyup(function() {

or

$('input[type="text"]').keyup(function() {

instead of

$(':text').keyup(function() {

Upvotes: 2

Related Questions