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
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
Share Improve this question edited Feb 9, 2012 at 10:26 nnnnnn 150k30 gold badges209 silver badges247 bronze badges asked Feb 9, 2012 at 10:15 MissCoder87MissCoder87 2,66910 gold badges49 silver badges82 bronze badges4 Answers
Reset to default 5Wele to javascript!
alert(1+1); -> 2
alert("1"+"1"); -> 11
alert(Number("1")+Number("1")); -> 2
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);
});
try using parseInt function http://www.w3schools./jsref/jsref_parseint.asp
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/faq/convert2.htm#parseInt