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

  • SEARCH
  • Home
  • 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 698327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:15:45+00:00 2026-05-14T03:15:45+00:00

Another jquery calculation question. I’ve this, which is sample code from the plugin site

  • 0

Another jquery calculation question.

I’ve this, which is sample code from the plugin site that I am playing with to get this working:

        function recalc(){
    $("[id^=total_item]").calc(
        "qty * price",
        {
        qty: $("input[name^=qty_item_]"), 
        price: $("input[name^=price_item_]"),
        },

        function (s){ return s.toFixed(2);},

        function ($this){ var sum = $this.sum();

            $("#grandTotal").val(
                // round the results to 2 digits
                sum.toFixed(2)
            );
        }
    );
}

Changes in the price fields cause the totals to update:

        $("input[name^=price_item_]").bind("keyup", recalc);
        recalc();

Problem is I won’t know the value of the price fields, they will be available to me only as a substring of values entered by the user, call it ‘itemcode’. There will be a variable number of items, based on a php query.

I’ve come up with this to change the price based on the itemcode:

    $("input[name^='itemcode_item_1']").keyup(function () {
    var codeprice = this.value.substring(2,6);
    $("input[name^='price_item_1']").val(codeprice);
});

$("input[name^='itemcode_item_2']").keyup(function () {
    var codeprice = this.value.substring(2,6);
    $("input[name^='price_item_2']").val(codeprice);
});

However while it does that, it also stops the item_total from updating. Also, I feel there must be a way to not need to write a numbered function for each item on the list. However when I just use

    $("input[name^='itemcode_item_']")

updating any itemcode field updates all price fields, which is not good.

Can anyone point me in the right direction? I know I am a bit clueless here, but javascript of any kind is not my thing.

Ok, updating this now to add some html. Basically though this is now just a little piece of sample stuff from the jquery calculator site, which I am using to get the functionality right before puttng it all into the actual functioning script. In reality there will be a variable number of items and they’ll be grouped in various ways.

<table>

            <tr><th>Qty</th><th align="left"></th><th>Item code</th><th>Price</th><th>Total</th></tr>
            <tr>
            <td><input type="text" name="qty_item_1" id="qty_item_1" value="1" size="3"/></td>
            <td>Item One</td>
            <td><input type="text" id="itemcode_item_1" name="itemcode_item_1" value="" size="8" /></td>
            <td><input type="text" id="price_item_1" name="price_item_1" value="" size="8" /></td>
            <td><input type="text" id="total_item_1" value=" " size="8"  readonly="readonly"  /></td>

            </tr>

            <tr>
            <td><input type="text" name="qty_item_2" id="qty_item_2" value="1" size="3"/></td>
            <td>Item Two</td>
            <td><input type="text" id="itemcode_item_2" name="itemcode_item_2" value="" size="8" /></td>
            <td><input type="text" id="price_item_2" name="price_item_2" value="" size="8" /></td>
            <td><input type="text" id="total_item_2" value=" " size="8" readonly="readonly" /></td>
            </tr>
            <tr>
            <td colspan="4" align="right"><strong>Grand Total:</strong></td>
            <td><input type="text" id="grandTotal" value=" " size="6" readonly="readonly">    </td>
            </tr>
        </table>
  • 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-14T03:15:45+00:00Added an answer on May 14, 2026 at 3:15 am

    You wrote that the elements will be grouped in various ways, but if this pattern is always kept (i.e. the related fields are inside one row):

    <tr>
      <td><input type="text" id="itemcode_item_1" name="itemcode_item_1" value="" size="8" /></td>
      <td><input type="text" id="price_item_1" name="price_item_1" value="" size="8" /></td>
    </tr>
    

    You can get your functionality this way:

    $("input[name^='itemcode_item_']").keyup(function () {
        var codeprice = this.value.substring(2,6);
       // closest(sel) finds the closest ancestor that matches 'sel'
       // find(sel) finds  all descendants that match the selector 'sel'
       $(this).closest('tr').find("input[name^='price_item_']").val(codeprice);
    });
    

    In general, try to figure out the realtion between the itemcode_item_ fields and their corresponding price_item_ field.
    In this case, they have a common ancestor tr and there is only on price_item_ input field inside it. Another relation would be, that price_item_1 is the first child of the next sibling of itemcode_item_1s parent.

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

Sidebar

Ask A Question

Stats

  • Questions 437k
  • Answers 437k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Turns out it was an issue with my page loading… May 15, 2026 at 4:09 pm
  • Editorial Team
    Editorial Team added an answer The underlying problem here is that the type being passed… May 15, 2026 at 4:09 pm
  • Editorial Team
    Editorial Team added an answer I like option 1. Also you don't need conditions and… May 15, 2026 at 4:09 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.