-1

$(document).ready(function() {
  $(".bubble").click(function() {
    // $("input").append(" <b>Appended text</b>");
    var x = document.getElementById("input").value;
    var y = x + 1;
    x = y;
    console.log(x);
    $('#input').val($('#input').val() + 1);
  });
});
.bg {
  height: 400px;
  background-color: #FFF1F1;
  /*display: inline-block;*/
  position: relative;
}
.bubble {
  height: 30px;
  width: 30px;
  border-radius: 50%;
  background-color: #24E93E;
  cursor: pointer;
  position: absolute;
  bottom: 0;
  left: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="container">
  <div class="row text-center">
    <div class="col-xs-3 btnDiv">
      <!-- <button type="button" class="btn btn-danger">STOP</button> -->
      <!-- <button type="button" class="btn btn-success">PLAY</button> -->
    </div>
    <div class="col-xs-6  bg main-block">
      <div class="bubble"></div>
    </div>
    <div class="col-xs-3 score-place">
      <input id="input" type="text" name="" placeholder="0">
    </div>
  </div>
</div>

Here on click, in input, it is starting from 1, but after that it is adding 1 like string. I need to do it as a math +. I am using their jQuery, but it is also ok to write the code just in JavaScript

Mike Cluck
  • 28,921
  • 12
  • 72
  • 85

2 Answers2

1

Parse the input as integer using parseInt() and keep referring to x

var x = document.getElementById("input").value;
var y = (parseInt(x, 10) || 0) + 1;
x = y;
console.log(x);
 $('#input').val(x); // do not add +1 again

$(document).ready(function(){
      $(".bubble").click(function(){
          // $("input").append(" <b>Appended text</b>");
          var x = document.getElementById("input").value;
          var y = (parseInt(x, 10) || 0) + 1;
          x = y;
          console.log(x);
          $('#input').val(x);
      });
  });
.bg{
 height: 400px;
 background-color: #FFF1F1;
 /*display: inline-block;*/
 position: relative;
}

.bubble{
 height: 30px;
 width: 30px;
 border-radius: 50%;
 background-color: #24E93E;
 cursor: pointer;
 position: absolute;
 bottom: 0;
 left: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
 <div class="container">
  <div class="row text-center">
   <div class="col-xs-3 btnDiv">
    <!-- <button type="button" class="btn btn-danger">STOP</button> -->
    <!-- <button type="button" class="btn btn-success">PLAY</button> -->
   </div>
   <div class="col-xs-6  bg main-block">
    <div class="bubble"></div>
   </div>
   <div class="col-xs-3 score-place">
    <input id="input" type="text" name="" placeholder="0">
   </div>
  </div>
 </div>
Adam Azad
  • 10,675
  • 5
  • 25
  • 63
-3

Use parseInt() in your javascript to convert the string to an integer.

$(".bubble").click(function() {
    $('#input').val(parseInt($('#input').val(), 10) + 1);
});

JSFiddle: https://jsfiddle.net/m52fwkjt/

Brian Moreno
  • 871
  • 2
  • 10
  • 32