Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8078395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:46:47+00:00 2026-06-05T15:46:47+00:00

How do you create a function in jQuery, call it, and pass a variable

  • 0

How do you create a function in jQuery, call it, and pass a variable to it? Here it is in Javascript style:

<script type="text/javascript">
function popup(msg) {
  alert(msg);
}
</script>

Then you would simply include the event in the HTML:

<html>
<input type="submit" onclick="popup('My Message')" />
</html>

What is the jQuery equivalent to doing this? From all the “newbie” tutorials, they are all static functions that do not accept variables. For example:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  $('button').click(function() {
    alert('hello, world');
   });
});
</script>

I want to be able to dynamically include different messages. The code above affects all buttons and spits out the same message of ‘hello, world’.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:46:49+00:00Added an answer on June 5, 2026 at 3:46 pm

    If you want the message to be dynamic, you’re going to want to use data-attributes:

    <input class='message-button-' type="submit" data-message="My Message" />
    

    Then in a separate javascript file:

    // equivalent of $(document).ready(function(){...
    $(function(){
    
      // Functionality starts here
      $('.message-button').on('click', function(e){
        alert($(this).data('message'));
      })
    
    
    });
    

    Now the function will execute any time a button with class .message-button is clicked, and it will use the custom attribute data-message to display a message.

    Hope this helps! 🙂

    Update:

    Note that it’s considered best-practice to do javascript unobstrusively. Some people have mentioned still using the onclick attribute like you did in your example, don’t do that. Bind the event like I’ve shown here using jquery’s .on('click', function(){}) method.

    If your click item is an anchor tag <a> then you can prevent the href from being followed if you want, by using e.preventDefault:

    $('a').on('click', function(e){
      e.preventDefault()
    })
    

    Also in case you need to know, the $('.message-button') is using css selectors to grab dom elements. Any valid CSS selector will normally work in there.

    Step-by-step

    First, we need to wrap all of our code in a function provided by jQuery that waits until the document is ready to handle javascript actions and dom manipulation. The long-form version is $(document).ready(function(){ /* CODE GOES HERE */ }) but the version I’ve used is a shortcut $(function({ /* CODE GOES HERE */ }):

    $(function(){
    })
    

    Next, we need to bind an event to our button. Since we’ve given it the class .message-button, we can use jQuery to grab that element and bind the ‘click’ event to it:

    $(function(){
      // Functionality starts here
      $('.message-button').on('click', function(e){
        // Code here executes when the input button is clicked
      })
    
    
    });
    

    Inside of event handlers this is re-bound to point directly to the HTML element that triggered the event. Wrapping in $() gives us access to jquery methods. Thus: $(this) is used very frequently. jQuery gives us the .data() method to access any data-* methods on the element and give us the value, which is what the alert() is doing.

    $(function(){
      // Functionality starts here
      $('.message-button').on('click', function(e){
        alert($(this).data('message'));
      })
    
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a popup for which I am using jQuery's CSS function.Here's
How can I create an update function in javascript/jquery? An update function is a
I'm trying to call a code-behind function inside JavaScript and pass a parameters to
I'm looking to create an abstracted jquery dialog function that i pass parameters into
I'm trying to create a JQuery function that will test a string for the
I developed a simple jQuery animation first and later I create a jQuery function
This didn't happen until I have added jQuery. function mfunc() {} mfunc.prototype.create = function(value)
I've created a wrapper function for jQuery's $.ajax() method so I can pass different
How can i create a function which looks like the jquery callback $ Say
I have a jquery Ajax call function that I use to submit form to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.