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 6944979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:22:37+00:00 2026-05-27T13:22:37+00:00

I need to make a jQuery function dynamic (parametrized). Currently i need to bind

  • 0

I need to make a jQuery function dynamic (parametrized). Currently i need to bind every form id with the jquery but I have more than a hundred forms ids so repeting the code would be meaningless just for same code but different id.

For eg:

Following is the form. Its id is “rat”

            <form id="rat" action="" method="post">

                <strong id="rating_title">Rate this: </strong>

                <select name="rate">
                    <?php foreach (get_options() as $id => $title): ?>
                        <option value="<?php echo $id ?>" <?php echo $id==3 ? 'selected="selected"' : '' ?> /><?php echo $title ?></option>
                    <?php endforeach; ?>
                </select>

                <input type="submit" value="Rate it!" />

            </form>

Below is the javascript attached this form

<script type="text/javascript">
    $(function(){
        $("#rat").children().not("select, #rating_title").hide();

        // Create caption element
        var $caption = $('<div id="caption"/>');

        // Create stars
        $("#rat").stars({
            inputType: "select",
            oneVoteOnly: true,
            captionEl: $caption,
            callback: function(ui, type, value)
            {
                // Display message to the user at the begining of request
                $("#messages").text("Saving...").fadeIn(30);

                // Send request to the server using POST method
                /* NOTE: 
                    The same PHP script is used for the FORM submission when Javascript is not available.
                    The only difference in script execution is the returned value. 
                    For AJAX call we expect an JSON object to be returned. 
                    The JSON object contains additional data we can use to update other elements on the page.
                    To distinguish the AJAX request in PHP script, check if the $_SERVER['HTTP_X_REQUESTED_WITH'] header variable is set.
                    (see: demo4.php)
                */ 
                $.post("demo4.php", {rate: value}, function(json)
                {
                    // Change widget's title
                    $("#rating_title").text("Average rating");

                    // Select stars from "Average rating" control to match the returned average rating value
                    ui.select(Math.round(json.avg));

                    // Update widget's caption
                    $caption.text(" (" + json.votes + " votes; " + json.avg + ")");

                    // Display confirmation message to the user
                    $("#messages").text("Rating saved (" + value + "). Thanks!").stop().css("opacity", 1).fadeIn(30);

                    // Hide confirmation message after 2 sec...
                    setTimeout(function(){
                        $("#messages").fadeOut(1000)
                    }, 2000);

                }, "json");
            }
        });

        // Since the <option value="3"> was selected by default, we must remove this selection from Stars.
        $("#rat").stars("selectID", -1);

        // Append caption element !after! the Stars
        $caption.appendTo("#rat");

        // Create element to use for confirmation messages
        $('<div id="messages"/>').appendTo("#rat");
    });
</script>

As you can see its binding to the “rat” form using $(“#rat”).

In the same manner I have around 100 forms with different ids and I need to bind each of every form in similar fashion…except for changes in few parameters.
For eg: if form id-“rat2” then $(“#rat2”)… $(“#messages”)… etc

Is there a way I can encapsulate the above entire javascript/jquery code to make it parametrized.

Please help!!!

  • 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-05-27T13:22:37+00:00Added an answer on May 27, 2026 at 1:22 pm

    Would it make more sense to add a class to each “rating” form, and just apply your code to each form with that class?

    <form class="rating" id="???">
      ...
    </form>
    

    Then add your jQuery:

    <script>
        $(function(){
            $('form.rating').each(function(){
                //apply logic to each form (extract id's from "this" form as needed)
            });
        });
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make some part of text bold while displaying in jquery: $(':checked').each(function()
I am new in jQuery and I have to do a dynamic form with
I have a crystal report file I need make a tiny edit in. It
I need to make an ArrayList of ArrayLists thread safe. I also cannot have
We need to make our enterprise ASP.NET/NHibernate browser-based application able to function when connected
Greetings all, I have a simple JQuery function which is bound to an ASP:Button
I have this jQuery function: $(this).change(function(){ alert('I changed. ID: ' + $(this).attr(id)); }); I
I'm currently working on a bit of jQuery that will allow users to make
I have multiple divs like setted up below. I want 1 (one) JQuery function
I have the need to pass an object from jstl to a Jquery click

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.