Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
How to call js functions defined in script tag of the erb file?

I have a layout for PDF (views/layouts/pdf.html.erb):

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
      function sayHello() {
        return "HAAHAHAhAHAAH"
      }
    </script>
    <style>
    </style>
  </head>
  <body>
    <%= yield %>
  </body>
</html>

I want to use that sayHello() function inside a p tag in app/views/invoices/pdf.html.erb, which gets embedded in yield of layouts/pdf.html.erb

I tried calling sayHello() in different ways like using <%= javascript: sayHello()%> etc.. but nothing works.

I also tried keeping sayHello() in a separate file and then referencing it in javascript_pack_tag in layouts/pdf.html.erb which leads to a different error.

Is there any way that I can access that function in html.erb?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Give your paragraph a class like so:

<p class="p_class"></p>

then put this

<script>
  document.getElementsByClassName('p_class')[0].textContent = sayHello();
</script>

at the bottom of your app/views/invoices/pdf.html.erb partial.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!