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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:50:09+00:00 2026-05-29T03:50:09+00:00

I have a textbox below which is created through this code: $(‘#txtWeight’).each( function() {

  • 0

I have a textbox below which is created through this code:

$('#txtWeight').each( function() {

     var $this = $(this);
     var $weightText = $("<input type='text' class='txtWeightRow' maxlength='5' onkeypress='return isNumberKey(event)'/>").attr('name',$this.attr('name'))
                     .attr('value',$this.val())

    $weight.append($weightText);

    });

What I want to know is that how is it suppose to be written that if the textbox is blank, then it should have a hidden value of 0?

Below is code which does the calculation between 100 and the number entered in the textbox:

function calculateTotal()
{
   var totalweight = totalmarks;
   $("#qandatbl td.weight input").each(function (i, elm){
        totalweight = totalweight - parseInt($(elm).val(), 10);
    });

    $("#total-weight").text(totalweight);
}

td.weight input is the textbox.

totalmarks = 100.

Thanks

  • 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-29T03:50:09+00:00Added an answer on May 29, 2026 at 3:50 am

    In the code that does the subtraction, you can treat an empty value as 0 by using the Number() constructor (a typecast). Like this:

    var $result = 100 - Number($('#txtWeight').val());
    

    Then it doesn’t matter what is in the textbox, the subtraction will work. If the value in the textbox is not a number (e.g. an empty string) it will be treated as the number zero, and the result of the subtraction will be 100 (not NaN).

    In your code, just use Number() instead of parseInt():

    function calculateTotal()
    {
        var totalweight = totalmarks;
        $("#qandatbl td.weight input").each(function (i, elm){
            totalweight = totalweight - Number($(elm).val());
        });
    
        $("#total-weight").text(totalweight);
    }
    

    If you want just to avoid decimal numbers, you can even do parseInt after the Number (i.e. totalweight = totalweight - parseInt(Number($(elm).val()), 10);).

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

Sidebar

Related Questions

I have created the TextBox and CompareValidator below which I thought would allow input
I have a few asp:textbox controls in a form on a webpage, below is
I have TextBox (multiline) and Label in an UpdatePanel which I refresh with javascript
I have a textbox with an onchange event. Why does this event not fire
I have a form which has a single textbox that sends some data to
I have created a javascript in which when we click Add button, it adds
Below is the report I have created. I would like to draw a line
I have created an application which converts Excel files to Access databases. During conversion
I have an XML below like this and I want to add in another
In my application I have TextBox in a FormView bound to a LinqDataSource like

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.