# Getting js variable value or form field value in js.rjs file

**URL:** <https://www.ruby-forum.com/t/getting-js-variable-value-or-form-field-value-in-js-rjs-file/189086>\
**Category:** Rails\
**Created:** [May 24, 2010, 11:16am UTC](https://www.ruby-forum.com/t/getting-js-variable-value-or-form-field-value-in-js-rjs-file/189086 "2010-05-24T11:16:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [May 24, 2010, 11:16am UTC](https://www.ruby-forum.com/t/getting-js-variable-value-or-form-field-value-in-js-rjs-file/189086/1 "2010-05-24T11:16:05Z")

</div>

Hi all

I have a file “index.js.rjs” where i only want to run the code in it  
(which updates the contents of a div in typical ajax style) if a  
variable i have from params is the same as a value of one of the form  
fields on the page (which has the id ‘search\_timestamp’). I can’t work  
out, though, how to get that value in my js.rjs file. Something like  
this

if @timestamp.to\_s == page[‘search\_timestamp’].val()  
#my code  
end

I feel like this should be easy but can’t figure it out. Can anyone  
show me how? I’ve tried just using page.call with some jquery but again  
can’t work out how to get the resultant value back into my ruby code.  
I’m using jquery and jrails in my app in case that’s relevant.

thanks, max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [May 24, 2010, 3:56pm UTC](https://www.ruby-forum.com/t/getting-js-variable-value-or-form-field-value-in-js-rjs-file/189086/2 "2010-05-24T15:56:30Z")

</div>

On May 24, 10:16 am, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> #my code  
> end
> 
> I feel like this should be easy but can’t figure it out. Can anyone  
> show me how? I’ve tried just using page.call with some jquery but again  
> can’t work out how to get the resultant value back into my ruby code.  
> I’m using jquery and jrails in my app in case that’s relevant.

At the point that your rjs file is run you can’t get the value of  
anything on user’s page, because it’s running on your server, not the  
user’s browser. I wrote a little about this a while ago at

> **[Conditional RJS explained - Space Vatican](https://www.spacevatican.org/2008/5/26/conditional-rjs-explained/)**
>
> RJS is one of those nifty little things that catches your eye when you first start off using Rails. You very quickly get to add ajax and whizzy …

Fred

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [May 24, 2010, 4:33pm UTC](https://www.ruby-forum.com/t/getting-js-variable-value-or-form-field-value-in-js-rjs-file/189086/3 "2010-05-24T16:33:41Z")

</div>

Frederick C. wrote:

> On May 24, 10:16ï¿½am, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ï¿½ #my code  
> > end
> > 
> > I feel like this should be easy but can’t figure it out. ï¿½Can anyone  
> > show me how? ï¿½I’ve tried just using page.call with some jquery but again  
> > can’t work out how to get the resultant value back into my ruby code.  
> > I’m using jquery and jrails in my app in case that’s relevant.
> 
> At the point that your rjs file is run you can’t get the value of  
> anything on user’s page, because it’s running on your server, not the  
> user’s browser. I wrote a little about this a while ago at  
> [Conditional RJS explained - Space Vatican](http://www.spacevatican.org/2008/5/26/conditional-rjs-explained)
> 
> Fred

Hi Fred - thanks, i should have known that. Looking at your great blog  
post, i adapted this approach:

page \<\< “if($(‘some\_div’).visible()){”  
page[‘some\_div’].replace :partial =\> ‘some\_partial’  
page \<\< ‘}else{’  
page.insert\_html :after, ‘something\_else’, :partial =\> ‘header’  
page \<\< ‘}’

like so:

page \<\< “if($(‘#search\_timestamp’).val() == #{params[:timestamp]}){”  
page.replace\_html “questions”, :partial =\> “questions/questions”  
page.replace “flashes”, :partial =\> “common/flashes”  
page \<\< “}”

and that’s worked a treat. Obviously it means i’m still generating the  
html from the partials every time, whether use it or not, but i think  
that’s the best that can be done here.

thanks agaain, max
