Skip to content Skip to sidebar Skip to footer

How To Output The Price When Clicking The Radio Button?

Does anyone know how to output the price when clicking the radio button ? Let say if i choose Small then it will display the output Rm2...for example Price = Rm 2....i have no ide

Solution 1:

You can pass as parameter this in calculate function(this refers to the element).

Using jquery:

functioncalculate(obj) {
  $("output").text(obj.value);
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><fieldsetstyle="width:240 px"><legend>Food order</legend><fieldsetstyle="width:240 px"><legend>Nasi Lemak</legend><p><inputtype="radio"name="j"value="2"onclick="calculate(this);" /><labelfor='small'class="inlinelabel">
        Small</label>
      (RM2)
    </p><p><inputtype="radio"name="j"value="3"onclick="calculate(this);" /><labelfor='regular'class="inlinelabel">
        Regular</label>
      (RM3)
    </p><p><inputtype="radio"name="j"value="4"onclick="calculate(this);" /><labelfor='large'class="inlinelabel">
        Large</label>
      (RM4)
    </p><tr><td>Price =</td><td><outputtype="number"name="price"id="output"></output></td></tr></fieldset>

Using plain js:

functioncalculate(obj) {
  document.getElementById("output").innerHTML = obj.value;
}
<fieldsetstyle="width:240 px"><legend>Food order</legend><fieldsetstyle="width:240 px"><legend>Nasi Lemak</legend><p><inputtype="radio"name="j"value="2"onclick="calculate(this);" /><labelfor='small'class="inlinelabel">
        Small</label>
      (RM2)
    </p><p><inputtype="radio"name="j"value="3"onclick="calculate(this);" /><labelfor='regular'class="inlinelabel">
        Regular</label>
      (RM3)
    </p><p><inputtype="radio"name="j"value="4"onclick="calculate(this);" /><labelfor='large'class="inlinelabel">
        Large</label>
      (RM4)
    </p><tr><td>Price =</td><td><outputtype="number"name="price"id="output"></output></td></tr></fieldset>

Solution 2:

Try it

document.getElementsByName("j").addEventListener("change", function(){
      document.getElementById("price").innerHTML = this.value;// Show the value in output element//orvar myRadioValue= this.value; //for some calculate
});

Post a Comment for "How To Output The Price When Clicking The Radio Button?"