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 :
<%= filter_link_helper “This
week” %>

<%= filter_link_helper “This
month” %>

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’;
}

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 :wink:
Regards
Stef

I thought the :success only refer to the div id to update in case on
success ?
Mickael.

Kad K. 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 :
<%= filter_link_helper “This
week” %>

<%= filter_link_helper “This
month” %>

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’;
}