SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Mar 2012
    Location
    Dhaka, Bangladesh
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Addition Problem

    How to solve the problem ???

    HTML Code:
    var bidincrement1= document.getElementById('bidincrement').value;
    var item_bid_amount2=document.getElementById('item_bid_amount').value;
    var bidincrement2=bidincrement1  +  item_bid_amount2;
    if
    bidincrement1=52
    item_bid_amount2=45

    I get 5245

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by innovasoftbd.com View Post
    if
    bidincrement1=52
    item_bid_amount2=45

    I get 5245
    The form values are strings, which is why they are being concatenated together.
    Make them numbers and things will go better for you. You can also use a default value of 0 if the value cannot be properly converted in to a number.

    For example:

    Code javascript:
    bidincrement1 = Number(bidincrement1) || 0;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •