简体   繁体   中英

In Rails how do I display javascript as text on an HTML page?

I have a JS Fiddle

I want to interpolate a small javascript script as text on a rails .erb view.

i want to take the input element from the js fiddle and store the javascript string as the value of the input element so that I can use the fiddle to copy the script into the clipboard.

The catch is this... I want to be able to interpolate some values into the string. ie

"function() {var arr = [<%= @rails_value %>]}"

Below is the code in which ruby data pass in js variable and add data to html page.

filename.html.erb

<div class="test"></div>

<script type="text/javascript">
  add_text()

  function add_text(){
    var value = '<%= @rails_value %>'
    $(".test").html(value)
  }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM