Question

I want to access the already selected radiobutton value in the second function call.

This is my code; can you please tell me where I am doing wrong? So when I click on checkbox I need to get already selected radio button value.

<input name="travel_type" id="arrival_1" type="radio" value="eclass_a" onClick="calculateTotal(this.value);checked="checked ">Arrivals $75
<input name="travel_type" id="dept_1" type="radio" value="eclass_d" onClick="calculateTotal(this.value); ">Departure $70
<input name="travel_type" id="pp_1" type="radio" value="eclass_p" onClick="calculateTotal(this.value);">Point to Point $70
<input name="travel_type" id="hourly_1" type="radio" value="eclass_h" onClick="calculateTotal(this.value);">Hourly $65

<input type="text" id="demo" name="demo" value="">

<b>Thsi is the second function call</b>

<input type="checkbox" style="border:none;" name="luggage_van" id="luggage_van" onClick="calculateTotal();getRadValue()" >Luggage Van
<input type="checkbox" style="border:none;" name="baby_seat" id="baby_seat" onClick="calculateTotal();getRadValue()" >Baby Seat
<input type="checkbox" style="border:none;" name="child_seat" id="child_seat" onClick="calculateTotal();getRadValue()">Child Seat
var globalvariable ;
function calculateTotal(id)
{
    globalvariable=document.getElementById('demo').value = id;
}
function getRadValue()
{
   var name = document.getElementById('demo').value = glovalvariable;
   alert(name);
}

So when I click on checkbox I need to get already selected radio button value.

Was it helpful?

Solution

Your second function getRadValue() isn't being called anywhere. Set it up:

<input type="checkbox" style="border:none;" name="luggage_van" id="luggage_van" onClick="getRadValue();" >

Fix your typo (glovalvariable vs globalvariable):

function getRadValue() {
    var name = document.getElementById('demo').value = globalvariable;
    alert(name);
}

Now when you click on the checkbox, you'll get an alert of your globalvariable.

Check for the existence of id in your first function:

function calculateTotal(id) {
    if (id) {
        globalvariable=document.getElementById('demo').value = id;
    }
    alert(globalvariable);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top