# Link\_to\_remote option :onsuccess, execute js function

**URL:** <https://www.ruby-forum.com/t/link-to-remote-option-onsuccess-execute-js-function/65332>\
**Category:** Rails\
**Created:** [August 17, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/link-to-remote-option-onsuccess-execute-js-function/65332 "2006-08-17T16:26:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 17, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/link-to-remote-option-onsuccess-execute-js-function/65332/1 "2006-08-17T16:26:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 17, 2006, 5:29pm UTC](https://www.ruby-forum.com/t/link-to-remote-option-onsuccess-execute-js-function/65332/2 "2006-08-17T17:29:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [May 18, 2009, 1:27pm UTC](https://www.ruby-forum.com/t/link-to-remote-option-onsuccess-execute-js-function/65332/3 "2009-05-18T13:27:26Z")

</div>

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