RoR Helper - Copy Input Value if Empty


/ Published in: Rails
Save to your folder(s)

I needed to copy the value of one field to a couple of others in case users decided to forget to fill them out. Using jQuery and added it to a helper because I was using in multiple places. Created variables to make it more readable.


Copy this code and paste it in your HTML
  1. def name_copy(model)
  2. '<script type="text/javascript" charset="utf-8">
  3. var n = $("input#' + model + '_name");
  4. var t = $("input#' + model + '_title");
  5. var st = $("input#' + model + '_seo_title");
  6. var su = $("input#' + model + '_seo_url");
  7.  
  8. n.blur(function(){
  9. if(t.val()==0){
  10. t.val(n.val());
  11. };
  12.  
  13. if(st.val()==0){
  14. st.val(n.val());
  15. };
  16.  
  17. if(su.val()==0){
  18. su.val(n.val());
  19. };
  20. });
  21. </script>'
  22. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.