1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Comma Error in JS Update

Discussion in 'JavaScript' started by medialab, Nov 29, 2019.

  1. #1
    Hey Everyone,

    I have a small piece of JS that updates a number inside of a div, it works great - until the number reaches 1,000 and a comma is added. Can anyone help with the code to fix this issue? I have tried quite a few option but everyone of them breaks the script.

    <script>
    function UpdatePoints(id,math) {
        jQuery.ajax({
            type: "POST",
            data: {number_id: id, math: math},
            url: "http://localhost/top10/parts/vote.php",
            cache: false,
            success: function(response)
        {
    
            if (math == 1) {
                $('#p'+id).html(function(i, val) { return val*1+1 });
                $('#point-up-'+id).hide();
                $('#point-down-'+id).hide();
                $('#vote-up-'+id).css('display','block')
            } else {
                $('#p'+id).html(function(i, val) { return val*1-1 });
                $('#point-up-'+id).hide();
                $('#point-down-'+id).hide();
                $('#vote-down-'+id).css('display','block')
            }
    
        }
        });
    }
    </script>
    Code (JavaScript):
    It is this line that's not working, all it is doing is grabbing the value and adding +1 (or -1 on the bottom half).

    $('#p'+id).html(function(i, val) { return val*1+1 });
    Code (JavaScript):
    Any help would be greatly appreciated!!
     
    medialab, Nov 29, 2019 IP
  2. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    818
    Best Answers:
    7
    Trophy Points:
    320
    #2
    I do not see where you are adding a comma to the number.
     
    mmerlinn, Nov 29, 2019 IP
  3. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #3
    In the JS I am not, it is pulling a number from a database and the PHP is adding the comma, works great until the number needs a comma.
     
    medialab, Nov 29, 2019 IP
  4. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    818
    Best Answers:
    7
    Trophy Points:
    320
    #4
    The answer is simple then. Remove the comma before doing the math.
     
    mmerlinn, Nov 29, 2019 IP
  5. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #5
    It does seem like the easiest solution - however on page load without the php adding the comma it just says 1000, they they have to click to run the JS and update the number.
     
    medialab, Nov 29, 2019 IP
  6. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    818
    Best Answers:
    7
    Trophy Points:
    320
    #6
    Remove the comma when you ENTER your function. If you need the comma in the result, add it back BEFORE leaving the function.
     
    mmerlinn, Nov 29, 2019 IP