Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery adding strings and not concatenating

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

like image 784
MissCoder87 Avatar asked Feb 09 '12 10:02

MissCoder87


1 Answers

Welcome to javascript!

alert(1+1); -> 2
alert("1"+"1"); -> 11
alert(Number("1")+Number("1")); -> 2
like image 162
georg Avatar answered Sep 20 '22 11:09

georg