Kad Kerforn
2006-Aug-17 14:31 UTC
[Rails] link_to_remote option :onsuccess, execute js function
How should I define the :onsuccess option in my - link_to_remote - tag, to execute a Js function I wrote : :success => "toggleButton(this, /db_bfilter/i);" ''this'' is the current element defined in my view : <span id="db_bfilter0" class="depressed"><%= filter_link_helper "This week" %></span> <span id="db_bfilter1" class="raised"><%= filter_link_helper "This month" %></span> when hit, the link execute a new filtered search (by week or by month according to the filter_link_helper ) on success it will toggle to class ''depressed'' and other links to class ''raised'' the search is correct, but links classes don''t toggle, as my :success option seems to be incorrect.... my js function toggle a button to simulate radio type buttons function toggleButton(elementObj, idRegex) { var arraySpans = document.body.getElementsByTagName("SPAN"); for(var i = 0; i < arraySpans.length; i++) { if(arraySpans[i].id.match(idRegex)) { arraySpans[i].className = ''raised''; } } elementObj.className = ''depressed''; } -- Posted via http://www.ruby-forum.com/.
Stef Telford
2006-Aug-17 15:27 UTC
[Rails] link_to_remote option :onsuccess, execute js function
Hello Kad, Well, your -assuming- that ''this'' will still be ''in scope'' and refer to the same javascript variable, yet the :success may not be able to reference it in that fashion. I dare say because its written with AJAX in mind and is thus ''asynchronous'' to the prior javascript and runs in its own little ''sandbox'' of a process. So, I think that your best solution is to store the id of the button clicked via the :before into a ''hidden variable'', and then inside your :success, get the element by id and use the value stored there as the object to update. Hopefully this makes some sort of sense :) Its early and I am still drinking my first cup of coffee ;) Regards Stef Kad Kerforn wrote:> How should I define the :onsuccess option in my - link_to_remote - tag, > to execute a Js function > I wrote : > :success => "toggleButton(this, /db_bfilter/i);" > > ''this'' is the current element defined in my view : > <span id="db_bfilter0" class="depressed"><%= filter_link_helper "This > week" %></span> > <span id="db_bfilter1" class="raised"><%= filter_link_helper "This > month" %></span> > > when hit, the link execute a new filtered search (by week or by month > according to the filter_link_helper ) > on success it will toggle to class ''depressed'' and other links to class > ''raised'' > > the search is correct, but links classes don''t toggle, as my :success > option seems to be incorrect.... > > my js function toggle a button to simulate radio type buttons > > function toggleButton(elementObj, idRegex) { > var arraySpans = document.body.getElementsByTagName("SPAN"); > for(var i = 0; i < arraySpans.length; i++) { > if(arraySpans[i].id.match(idRegex)) { > arraySpans[i].className = ''raised''; > } > } > elementObj.className = ''depressed''; > } > >
Mickael Faivre-Macon
2009-May-18 11:27 UTC
Re: link_to_remote option :onsuccess, execute js function
I thought the :success only refer to the div id to update in case on success ? Mickael. Kad Kerforn wrote:> How should I define the :onsuccess option in my - link_to_remote - tag, > to execute a Js function > I wrote : > :success => "toggleButton(this, /db_bfilter/i);" > > ''this'' is the current element defined in my view : > <span id="db_bfilter0" class="depressed"><%= filter_link_helper "This > week" %></span> > <span id="db_bfilter1" class="raised"><%= filter_link_helper "This > month" %></span> > > when hit, the link execute a new filtered search (by week or by month > according to the filter_link_helper ) > on success it will toggle to class ''depressed'' and other links to class > ''raised'' > > the search is correct, but links classes don''t toggle, as my :success > option seems to be incorrect.... > > my js function toggle a button to simulate radio type buttons > > function toggleButton(elementObj, idRegex) { > var arraySpans = document.body.getElementsByTagName("SPAN"); > for(var i = 0; i < arraySpans.length; i++) { > if(arraySpans[i].id.match(idRegex)) { > arraySpans[i].className = ''raised''; > } > } > elementObj.className = ''depressed''; > }-- Posted via http://www.ruby-forum.com/.
Possibly Parallel Threads
- Prototype Ajax.Request (onLoading and onSuccess) question
- onSuccess and onComplete Prototype callbacks
- Ajax.Request synchronization (onSuccess before onComplete?)
- [Prototype] onSuccess Event and Ajax.Updater bug in IE
- RE: [Prototype] onSuccess Event and, Ajax.Updater bug in IE