3

In a view I have a script (javascript) that declares a variable after some deep processing. How can I use that variable outside the script boundaries in the same view page?

some_view.html.erb

<script>var seq = Sortable.sequence('list');</script>

# How to do this? Is it possible?

<%= sortable_element "list",
  :update => "order",
  :complete=> visual_effect(:highlight, "list"),
  :scroll => 'list',
  :url=>{:action=>"order", :order_init => seq} %>

Thank you!

1
  • I am sorry I am a bit thick tonight. Maybe this helps explaining myself. I am trying to do something like this: <script>var seq = Sortable.sequence('list');</script> <%= sortable_element "list", :update => "order", :complete=> visual_effect(:highlight, "list"), :url=>{:action=>"order", :order_init => seq} %> Commented Feb 11, 2010 at 18:47

2 Answers 2

2

It's not possible becasue Rails is a server-side language and javascript is executed on client side.

What you are looking for is AJAX functionality (asynchronous JavaScript)

Sign up to request clarification or add additional context in comments.

Comments

0

If it is truly javascript that declares the variable after JavaScript processing, then you need to work the other way around. Like this (Move the script block below the HTML or this example will fail):

<h2> Hello, my name is <span id="name"></span></h2>

<script type="text/javascript">
   var name = "World";
   document.getElementById('name').innerHTML = name;
</script>

That way after the script runs, the name would be replaced in the HTML on the client side.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.