Ahm.Nisa
Ahm.Nisa

Reputation: 13

How to Get the Result Using Javascript

I am working with this code and would like to get the result inside intTotal. But it doesn't give me the result I wish.

window.onload = function() {
  var intNum = document.getElementById("intNum");
  for (var i = 1; i <= 3000; i++) {
    var option = document.createElement("option");
    option.innerHTML = i;
    option.value = i;
    intNum.appendChild(option);
  }
};

function jml_total() {
  var hrg_sat = parseInt(document.querySelector("hrg_sat").innerHTML);
  var jml_pilih = parseInt(document.getElementById("intNum").value);
  var int_hitung = hrg_sat * jml_pilih;
  document.getElementById("intTotal").value = int_hitung;
}
<div id="hrg_sat">3000</div>
<select id="intNum" onChange="jml_total(this.value)">
  <option value="" disabled selected>Pilih Jumlah ... </option>
</select>
<br />
<div id="intTotal"></div>

I tried this thread. Or, you have the simplest one, but not using jquery.

Upvotes: 1

Views: 52

Answers (2)

95faf8e76605e973
95faf8e76605e973

Reputation: 14191

You need to specify that hrg_sat is an id on your querySelector by using # as prefix

var hrg_sat = parseInt(document.querySelector("#hrg_sat").innerHTML);

Also, as the previous answer has mentioned, you need to use innerHTML property instead of value to display the text on the DOM on a <div> element

window.onload = function() {
  var intNum = document.getElementById("intNum");
  for (var i = 1; i <= 3000; i++) {
    var option = document.createElement("option");
    option.innerHTML = i;
    option.value = i;
    intNum.appendChild(option);
  }
};

function jml_total() {
  var hrg_sat = parseInt(document.querySelector("#hrg_sat").innerHTML);
  var jml_pilih = parseInt(document.getElementById("intNum").value);
  var int_hitung = hrg_sat * jml_pilih;
  document.getElementById("intTotal").innerHTML = int_hitung;
}
<div id="hrg_sat">3000</div>
<select id="intNum" onChange="jml_total(this.value)">
  <option value="" disabled selected>Pilih Jumlah ... </option>
</select>
<br />
<div id="intTotal"></div>

Upvotes: 3

chrisbyte
chrisbyte

Reputation: 1608

Since intTotal is a div, you want to use innerHTML not value. value in the post you linked to is for an input control, not a div

document.getElementById("intTotal").innerHTML = 'your content goes here';
<div id="intTotal"></div>

Upvotes: 0

Related Questions