0

at the moment I use this code to disable submit button until all input fields in the form are filled out. Altough I would like to add refresh on keyup, so it would enable the button as soon as you finish typing in the last inputbox...

function buttonState(){ $("input").each(function(){ $('#submit').attr('disabled', 'disabled'); if($(this).val() == "" ) return false; $('#submit').attr('disabled', ''); }) } $(function(){ $('#submit').attr('disabled', 'disabled'); $('input').change(buttonState); }) 
3
  • 3
    Please try to do that first. It's not difficult. Show some interest. Commented Dec 11, 2011 at 12:32
  • Thanks for excellent advice, i would not post if this wouldn't be an issue for me. Commented Dec 11, 2011 at 12:56
  • The function is not .onkeyup(fn), it is just plain .keyup(fn). See the jquery docs. api.jquery.com/keyup Commented Dec 11, 2011 at 13:01

3 Answers 3

1

Edited

$(function(){ function buttonState(){ var isValid = true; $("input").each(function(){ if($(this).val() == "" ) isValid = false; }); if (isValid) $('#submit').attr('disabled', ''); else $('#submit').attr('disabled', 'disabled'); } $('#submit').attr('disabled', 'disabled'); $('input').on('change keyup', buttonState); }) 
Sign up to request clarification or add additional context in comments.

2 Comments

What version of jquery are you using?
Since you've introduced the isValid flag, why don't you move the disabling out of the each() loop too? Only need to enable/disable once after the loop.
0

Have you tried

$('#submit').removeAttr('disabled'); 

instead of

$('#submit').attr('disabled',''); 

?

Not sure if that does matter, but might make for clearer markup anyway.

You said 'Doesnt' work for some reasson'... can you be more precise? Does it submit either way, or does the button not get enabled state?

Comments

0

AFAIK i do this to set disabled attribute on form element :

$('#submit').attr('disabled',true); //to set it disabled $('#submit').attr('disabled',false); // to release the disabled attr 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.