Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery enable submit button on input value change

Tags:

jquery

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)

like image 882
damien Avatar asked Jul 22 '10 12:07

damien


3 Answers

You can do something like this:

$('input[name="age"], input[name="poscode"]').change(function(){
  if ($(this).val())
  {
    $("input[name='submit']").removeAttr('disabled');
  }
});
like image 128
Sarfraz Avatar answered Sep 20 '22 19:09

Sarfraz


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');
}
});
like image 21
Lo Juego Avatar answered Sep 19 '22 19:09

Lo Juego


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

http://jsfiddle.net/9sxwN/

like image 29
Matthew J Morrison Avatar answered Sep 19 '22 19:09

Matthew J Morrison