anu
anu

Reputation: 25

How to get the value of a html span element in jquery?

I have a span element in my html as shown below containing a value 0 :

<span class="personcart">0</span>

I have written a jquery to add 1 to the number stored in the span element.When I am on a button which is span element containing class as addtocartbt. I am adding 1 to the zero value in the span element with class personcart and storing 1 as the content of span element with class personcart. But when I am again click the span element with class addtocartbt, it alerting the value as 11 which is previously stored in the span element with class name personcart. Actually it should be 1 only not 11.

var totalsum = parseInt($("span.personcart").text());
$("span.addtocartbt").click(function()
{




      alert(totalsum);
      var intg = 1;

     if(totalsum == 0){



        var totalsum   = parseInt(totalsum) + parseInt(intg);
        //$("span.personcart").html("0");
        alert(totalsum);
        $("span.personcart").html(totalsum);


      }
      else{
            alert("okkkk!!!");
            var totalsum = parseInt($("span.personcart").text());
            //$("span.personcart").html("0");
            var totalsum   = parseInt(totalsum) + parseInt(intg);
            alert(totalsum);
            $("span.personcart").html(totalsum);

      }

      /*var totalsum = 0;
      alert(totalsum);
      var totalsum   = parseInt(totalsum) + 1;
      alert(totalsum);*/

});

Can anyone say how to do this ?

Upvotes: 0

Views: 1560

Answers (2)

guradio
guradio

Reputation: 15565

var totalsum = parseInt($("span.personcart").text());
$("span.addtocartbt").click(function() {
  alert(totalsum);
  var intg = 1;
  if (totalsum == 0) {
    totalsum = parseInt(totalsum) + parseInt(intg);//dont initialize
    //$("span.personcart").html("0");
    alert(totalsum);
    $("span.personcart").html(totalsum);
  } else {
    alert("okkkk!!!");
    totalsum = parseInt($("span.personcart").text());
    //$("span.personcart").html("0");
    totalsum = parseInt(totalsum) + parseInt(intg);//dont initialize
    alert(totalsum);
    $("span.personcart").html(totalsum);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="personcart">0</span>
<span class="addtocartbt">addtocartbt</span>

Commented the places where you reinitialize the variable do check the comment

Upvotes: 0

Tushar_G
Tushar_G

Reputation: 300

it's working already

$("span.addtocartbt").click(function()
{



     var totalsum = parseInt($("span.personcart").text());
      alert(totalsum);
      var intg = 1;

     if(totalsum == 0){



        var totalsum   = parseInt(totalsum) + parseInt(intg);
        //$("span.personcart").html("0");
        alert(totalsum);
        $("span.personcart").html(totalsum);


      }
      else{
            alert("okkkk!!!");
            var totalsum = parseInt($("span.personcart").text());
            //$("span.personcart").html("0");
            var totalsum   = parseInt(totalsum) + parseInt(intg);
            alert(totalsum);
            $("span.personcart").html(totalsum);

      }



 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="personcart">0</span>
<span class="addtocartbt">Add To Cart</span>

Upvotes: 1

Related Questions