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

136
Views
Call jQuery function from HTML

I have this code and I am trying do call a function, but it doesn't work. Can anyone tell me what I need to do to fix this?

$(document).ready(function() {
  function BoxMsg(mess) {
    $.MessageBox(mess);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<input id="rxc021" name="rxc021" type="button" class="e-btn e-btn-border" value="Ask" onclick="BoxMsg('rxc021 - option 1')">
<input id="rxc022" name="rxc022" type="button" class="e-btn e-btn-border" value="Ask" onclick="BoxMsg('rxc022 - option 2')">
<input id="rxc023" name="rxc023" type="button" class="e-btn e-btn-border" value="Ask" onclick="BoxMsg('rxc023 - option 3')">
<input id="rxc024" name="rxc024" type="button" class="e-btn e-btn-border" value="Ask" onclick="BoxMsg('rxc024 - option 4')">

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

For functions referenced by onclick attributes they need to be placed in global scope. As your function is defined in document.ready, it's not in scope. To fix the problem, move your function definition:

function BoxMsg(mess) {
  $.MessageBox(mess);
}
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/gasparesganga-jquery-message-box@3.2.2/dist/messagebox.min.js"></script>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gasparesganga-jquery-message-box@3.2.2/dist/messagebox.min.css" />
<input id="rxc021" name="rxc021" type="button" class="e-btn e-btn-border" value="Ask" onclick="BoxMsg('rxc021 - option 1')">
<input id="rxc022" name="rxc022" type="button" class="e-btn e-btn-border" value="Ask" onclick="BoxMsg('rxc022 - option 2')">
<input id="rxc023" name="rxc023" type="button" class="e-btn e-btn-border" value="Ask" onclick="BoxMsg('rxc023 - option 3')">
<input id="rxc024" name="rxc024" type="button" class="e-btn e-btn-border" value="Ask" onclick="BoxMsg('rxc024 - option 4')">

Better still, do not use onclick. It's outdated and not good practice.

The better alternative is to use unobtrusive event handlers bound in your JS code, not the HTML. You can use data attributes to retrieve element metadata within these event handlers:

jQuery($ => {
  $('.e-btn').on('click', e => {
    let $btn = $(e.target);
    $.MessageBox($btn.data('message'));
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gasparesganga-jquery-message-box@3.2.2/dist/messagebox.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gasparesganga-jquery-message-box@3.2.2/dist/messagebox.min.css" />
<input id="rxc021" name="rxc021" type="button" class="e-btn e-btn-border" value="Ask" data-message="rxc021 - option 1" />
<input id="rxc022" name="rxc022" type="button" class="e-btn e-btn-border" value="Ask" data-message="rxc022 - option 2" />
<input id="rxc023" name="rxc023" type="button" class="e-btn e-btn-border" value="Ask" data-message="rxc023 - option 3" />
<input id="rxc024" name="rxc024" type="button" class="e-btn e-btn-border" value="Ask" data-message="rxc024 - option 4" />

about 4 years ago · Santiago Trujillo 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!