Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 590
Sri Avatar asked Dec 06 '22 12:12

Sri


2 Answers

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);
like image 171
Adil Avatar answered Dec 11 '22 11:12

Adil


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.

like image 24
iouri Avatar answered Dec 11 '22 11:12

iouri