Sri
Sri

Reputation: 2273

how to disable a submit button if text_field is empty in ruby on rails?

I am new to ROR. I just want to know is it possible to disable a submit tag button if the text_field is empty.??

thanks

Upvotes: 4

Views: 7617

Answers (3)

Adil
Adil

Reputation: 148180

You can do it with jquery like this,

Live Demo

if($('#text_field').val() ==  "") 
   $('#submitButtonId').attr('disabled', true);

$('#text_field').keyup(function(){
    if($('#text_field').val() !=  "") 
         $('#submitButtonId').attr('disabled', false);    
    else
         $('#submitButtonId').attr('disabled', true);   
});

For latest version of jQuery you may need to use prop() instead of attr() to set the disabled property of the element.

if($('#text_field').val() ==  "") 
   $('#submitButtonId').prop('disabled', true);

Upvotes: 14

iouri
iouri

Reputation: 2929

Typically it's done through validations, so the button stays active but the form gets validation errors and doesn't save. In your model you would add:

validates_presence_of :some_field, :some_other_field

If you want to do it anyway, you would use javascript to accomplish it.

Upvotes: 1

Sushanth --
Sushanth --

Reputation: 55750

You can use jQuery for that

$(function(){

     var val = $('#text_field').val();
     if(val == ''){
        $('input[type=submit]').attr('disabled', true)
     }
});

Check FIDDLE

Upvotes: 0

Related Questions