K
K
Kyrat Farre2015-10-13 17:25:01
PHP
Kyrat Farre, 2015-10-13 17:25:01

How to display a digital (numerical) value from a checkbox using js?

I have the most banal checkbox, I need that when I click on this square, I would display the tittle value of this checkbox in a certain place (there is a tag <p>with id = "result"), that is, I marked the checkbox, the amount increased for example by 100r.
I tried it, it gives me the Nan value.

<div class="kadzakad" action="" method="post">
<input title="53" onchange="calc()" type="radio" name="kad_1" id="kad_1">
  <label class="kad" for="kad_1"><b>Внутри КАД/</b></label>
</div>

function calc() {           
    var ter = document.getElementById("kad_1").checked;
    var result = document.getElementById("result");
    var price = 100;

    price =parseInt(ter.title);

    result.innerHTML = price;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Inchin ☢, 2015-10-13
@Webmast2105

var ter = document.getElementById("kad_1").checked; //true или false
price =parseInt(ter.title); //  parseInt(undefined)

Were you crazy?
var ter = document.getElementById("kad_1"),
      price = parseInt(ter.title); //А лучше просто +ter.title

Mini code rewrite
function calc() {
  var result = document.getElementById("result");
  //this и так document.getElementById("kad_1");              
  
  result.innerHTML = +this.title;
}

K
keslo, 2015-10-13
@keslo

NaN is not a number. Most likely you are not getting a number somewhere.
You can get the title value like this:

$("#kad_1").click(function() {
    // получаем значение title
    var title = $(this).attr("title");

     // вставляем данные в result
    $("#result").html(title);
});

If you need to increase the number in result, then:
var result = +("#result").html() + title;
 $("#result").html(result);

But in this case, if the user unchecks the checkbox, will the final cost need to be reduced? Then use checked.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question