but inside the view? In the view, I have a script (javascript) that declares ...">

Rails: How to use javascript variable outside of <script ... / "> but inside the view?

In the view, I have a script (javascript) that declares a variable after some deep processing. How to use this variable outside the borders of the script on 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!

+3
source share
2 answers

This is not possible because Rails is a server-side language, and javascript is executed on the client side.

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

+2
source

javascript, JavaScript, . ( script HTML ):

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

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

, script HTML .

0

Source: https://habr.com/ru/post/1732397/


All Articles