Reputation: 503
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!
Upvotes: 3
Views: 923
Reputation: 66191
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.
Upvotes: 0
Reputation: 18523
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)
Upvotes: 2