I have created three radio buttons. I want to run a function OnClick that ask the user to confirm the chosen radio button and tell what the checked radio button value is. Also if the user doesn't confirm the checked radio button, the radio button will be deselected. This is code i have written but failed.
<input id="a1" type="radio" name="type" value="A1" onClick= "confirmation();" />A1
<input id="a2" type="radio" name="type" value="A2" onClick="confirmation();" />A2
<input id="a3" type="radio" name="type" value="A3" onClick="confirmation();"/>A3
Javascript
function confirmation() {
if (document.getElementById('a1').checked ) {
ty = document.getElementById('a1').value
var ask= confirm("You have chosen " + ty + " as your type \n If you have chosen the right type, Click Ok! " )
}
if (document.getElementById('a2').checked) {
level = document.getElementById('a2').value;
var ask= confirm("You have chosen " + ty + " as your Examination Level \n If you have chosen the right type, Click Ok! " )
}
if (document.getElementById('a3').checked) {
ty = document.getElementById('r1').value;
var ask= confirm("You have chosen " + ty + " as your type \n If you have chosen the right type, Click Ok! " )
}
if (ask==true) {
alert("You clicked ok");
}
if (ask==false) {
alert("You clicked cancel");
}
}
To check which radio button is selected in a form, we first get the desired input group with the type of input as an option and then the value of this selection can then be accessed by the val() method. This returns the name of the option that is currently selected.
However, if you want to check that a specific radiobutton is checked or not, you can use Get element attribute to fetch the value of the "checked" attribute. It will return "true" if it is checked. You received this message because you are subscribed to the Google Groups "robotframework-users" group.
We can select a radio button; the click operation needs to perform. So, once we locate the element, we need to click to select it. Therefore, we can select a Radio Button which has the unique "id " attribute. And, select the same by using the "click " operation.
I think you can sum it up to this:
function confirmation(obj) {
obj.checked ? confirm("You have chosen " + obj.value + " as your type \n If you have chosen the right type, Click Ok!") ? alert("You clicked ok") : obj.checked = false : "";
}
<input id="a1" type="radio" name="type" value="A1" onClick="confirmation(this);" />A1
<input id="a2" type="radio" name="type" value="A2" onClick="confirmation(this);" />A2
<input id="a3" type="radio" name="type" value="A3" onClick="confirmation(this);" />A3
Pass this
to the function. this
refers to DOM object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With