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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:54:04+00:00 2026-06-01T11:54:04+00:00

I have a function that adds change events for form items based on the

  • 0

I have a function that adds change events for form items based on the row name(uses the database to get these).

Heres the current function:

    $sql = "SELECT * FROM product WHERE storeno = '1' ORDER BY descript";
    $result = mssql_query($sql, $msConnection);

    if ($result && mssql_num_rows($result) > 0) {
        while ($row = mssql_fetch_object($result)) {
            $sku = trim($row->mas90sku);
            $JQueryReadyScripts .= "
            $('#newCount_" . $sku . "_row .cases').change(function() {
                var count = 0;
                $('#newCount_" . $sku . "_row .cases').each(function () {
                    count += parseFloat($(this).val());
                });
                $('#cases_total_" . $sku . "').text(count);
            });
            $('#newCount_" . $sku . "_row .units').change(function() {
                var count = 0;
                $('#newCount_" . $sku . "_row .units').each(function () {
                    count += parseFloat($(this).val());
                });
                $('#units_total_" . $sku . "').text(count);
            });";
        }
        mssql_free_result($result);
    }

How can I consolidate this into a call that affects every row so that I can get rid of the DB portion and have just 2 pieces (or 1) of code instead of 2 for every row.

  • 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-01T11:54:05+00:00Added an answer on June 1, 2026 at 11:54 am
    $('*[id^=newCount_]').each(function () {
      var $row = $(this), 
          sku  = this.id.replace(/newCount_(.*?)_row/, '$1');
    
      function sumValues($elems) {
        var sum = 0;
        $elems.each(function () { sum += parseFloat(this.value) });
        return sum;
      }
    
      $.each(['cases', 'units'], function (i, type) {
        $row.on('change', '.' + type, function () {
          $('#' + type + '_total_' + sku).text( sumValues($row.find('.' + type)) );
        });
      });
    });
    

    This has no dependency on PHP, just use it as a regular script.

    It uses .on() event delegation, so it requires jQuery 1.7+. For earlier versions of jQuery you can use .delegate() for the same effect.

    I’d recommend giving all your rows a common CSS class, so that the not-so-nice $('*[id^=newCount_]') can be replaced with something simpler, but that’s a cosmetic change.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that adds eventhandlers to control events. Right now I have
I have a function that dynamically adds events to the calendar. function AddEventSourceDetailed(act_id) {
I have the following structure and function that adds things to the structure: struct
I have such function, that adds a grid of droppables: function AddClassroomDrops(grid, weeks, days,
I have this function, that adds 31px to my style bottom on ul. It
I have a javascript function that manipulates the DOM when it is called (adds
I have an html form. When you click the button, a javascript function adds
I have some JavaScript that creates a row in a table and adds cells
I have a third-party js function that dynamically adds/removes options to a SELECT element
i have a form that with a link click adds a new for a

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.