5

I'm adding values from select lists (or subtracting) and I can't seem to make it not concatenate the string.

My select list is:

<li class="extraHeight">
    <asp:Image runat="server" CssClass="iconImageMove" ID="Image13" ImageUrl="~/images/icons/ABabyChg_Off.png" />
    <div class="flipWidth">
        <select name="access" class="change" id="ABabyChg_Off"  runat="server">
            <option value="16777216">Off</option>
            <option value="16777216">On</option>
        </select>
     </div> <br /> <p class="noWrap">Accessible baby changing facilities</p>
</li>

<li class="extraHeight">
    <asp:Image runat="server" CssClass="iconImageMove" ID="Image14" ImageUrl="~/images/icons/carpark_off.png" />
    <div class="flipWidth">
        <select name="access" class="change" id="carpark_off"  runat="server">
            <option value="256">Off</option>
            <option value="256">On</option>
        </select>
     </div> <br /> <p class="noWrap">Accessible Car parking facilities</p>
</li>

And my javascript is:

<script>    

        $("select").change(function () {

           var currentAccess = "0";
           var currentText = $(":selected", this).text();
           var value = $(this).val()
           alert(currentAccess);
            if(currentText == "Off")

                    {
                        currentAccess -= value;
                    }  
            if(value != "0") {        
            if(currentText == "On")
                     {
                        currentAccess += value;
                     }
            }

              $.cookie("accessReqs",  currentAccess, { expires: 24 });
              alert(currentAccess);

            })

    </script>

Basically I'm trying to have currentAccess to have the current value as an integer and add and subtract as is required.

Tom

4 Answers 4

5

Welcome to javascript!

alert(1+1); -> 2
alert("1"+"1"); -> 11
alert(Number("1")+Number("1")); -> 2
Sign up to request clarification or add additional context in comments.

Comments

4

Use integers instead of strings.

  • Initialize currentAccess at zero.
  • The -= operator leaves little place for ambiguity, but the
    += operator can either mean "concatenate a string" or "add a number".
  • To make it more obvious, convert the variable to a number, eg by using *1 (times 1)

Updated code:

$("select").change(function () {

    var currentAccess = 0; // Instead of "0"
    var currentText = $(":selected", this).text();
    var value = $(this).val() * 1;
    alert(currentAccess);
    if (currentText == "Off")
    {
        currentAccess -= value;
    }
    if (value != 0) {
        if (currentText == "On") {
            currentAccess += value;
        }
    }

    $.cookie("accessReqs", currentAccess, {
        expires: 24
    });
    alert(currentAccess);    
});

5 Comments

Some consider *1 (instead of Number/parseInt) bad style.
@thg435 [citation needed]. Number is a function, and much slower than a plain *1.
Thanks for this. When I add the value to the cookie, then read the cookie back it concatinates it again and loosing it as a value. I've tried parseInt but it doesn't seem to do much
@TomBeech Have you applied all changes by me? When currentAccess is a string, += value will concatenate strings, regardless of the value's type. You can use currentAccess = +currentAccess + 1*value; instead. Have a look at this answer for a comparison between all number conversion methods.
@RobW: I can't imagine a javascript application where Number() would be a bottleneck. Do you have any real-life example at hand?
1

try using parseInt function http://www.w3schools.com/jsref/jsref_parseint.asp

Comments

1

Use parseInt() to convert your value in string format to integer and then perform arithmetic operation on them.

More info on parseInt http://www.javascripter.net/faq/convert2.htm#parseInt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.