0

My javascript is:

$(function(){
  $("a[data-toggle-length]='toggle'").click(function(event){
    event.preventDefault();
    $("span.show_hide").toggleClass("shown hidden");
    $("table").toggleClass("wide narrow");
    // here  
    $.get('/toggle_full_details');
  }); 
});

I want to add to this js, something like (just before the $.get call above):

if (params[:focus_on_first_field]) == "1" { 
  $("input[type='text']:first").focus();
}   

But params[:focus_on_first_field] is the rails way to refer to a url parameter and gives me a syntax error here.

How can I refer to the url param in javascript? I can use jquery if it makes it easier. I already use it elsewhere.

The haml view is;

= render partial: 'links', :locals => {:first_field_focus => 1 }

which calls a partial which uses:

- this_first_field_focus = ((first_field_focus ||= 0) == 1) ? 1 : 0
...
= render partial: 'toggle_details_link_bold', :locals => {:txt => "Summary / 
<b>Details</b>",     :show_hide => 'shown', :first_field_focus => this_first_field_focus}

and then uses another partial, which has the hyperlink:

%a{href: '#', :data => {toggle_length: 'toggle', first_field_focus: first_field_focus}}

I tried using

...
// here
if ($(location).attr('data-first-field-focus') == "1") {
  $("input[type='text']:first").focus();
} 
...

but it didn't work (but no console errors either)

Without the attr condition, $("input[type='text']:first").focus(); does work on its own.

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
  • Yeah, you can do AJAX calls. Create a helper method that returns params[:focus_on_first_field] and use ajax to call this method. – donkey Aug 24 '14 at 17:52
  • Refer to: http://stackoverflow.com/questions/979975/how-to-get-the-value-from-url-parameter – Inkling Aug 25 '14 at 04:48

0 Answers0