Explain How To Evaluate Radiobutton in JavaScript With Suitable Example.
Explain How To Evaluate Radiobutton in JavaScript With Suitable Example.
→ Evaluating radio buttons in JavaScript means accessing their state
(checked or unchecked) and taking action based on that state. Here are two
common ways to achieve this:
● R adio buttons have a built-in checked property that returns true if the
button is selected and false otherwise.
● You can use this property to check the state of a radio button element.
Here's an example:
HTML
inputtype=" radio"name="fruit"value=" apple"id=" apple">Apple
<
<inputtype=" radio"name="fruit"value=" banana"id="banana">Banana
<buttononclick="checkFruit()">Check Selected Fruit</button>
script>
<
functioncheckFruit() {
if(d ocument.getElementById("apple").checked) {
alert("Apple is selected");
}elseif(d ocument.getElementById("banana").checked) {
alert("Banana is selected");
}else{
alert("No fruit selected");
}
}
</script>
● Y ou can use these methods to select radio buttons based on specific criteria,
such as their name or id.
● Once you have selected the radio buttons, you can access their checked
property as before.
Here's an example:
HTML
inputtype=" radio"name="size"value=" small"id=" small">Small
<
<inputtype=" radio"name="size"value=" medium"id=" medium">Medium
<inputtype=" radio"name="size"value=" large"id=" large">Large
<buttononclick="checkSize()">C heck Selected Size</button>
script>
<
functioncheckSize() {
constselectedSize =document.querySelector('input[name="size"]:checked');
if(selectedSize) {
alert("Selected size: "+ selectedSize.value);
}else{
alert("No size selected");
}
}
</script>
Additional points:
● Y ou can use event listeners like onchange to dynamically update your code
based on the radio button's state.
● Consider using unique name attributes for radio buttons within a group to
ensure only one can be selected at a time.
● Remember to handle the scenario where no radio button is selected in your
evaluations.
HTML
<!DOCTYPE html>
html
<
lang="en">
<head>
meta
<
charset="UTF-8">
meta
<
name=" viewport"
content="width=device-width, initial-scale=1.0">
<title>PullDown Menu</title>
/head>
<
<body>
<h1>S elect a website:</h1>
<selectid="website-select">
<optionvalue="https://aicte-india.org/">A ICTE</option>
<optionvalue="https://www.dte.org.in/">D TE</option>
<optionvalue="https://msbte.org.in/">M
SBTE</option>
<optionvalue="https://www.google.com/">G OOGLE</option>
</select>
<buttononclick=" redirectWebsite()">G
o to website</button>
<script>
functionredirectWebsite() {
constselectedWebsite =
document.getElementById("website-select").value;
window.location.href = selectedWebsite;
}
</script>
</body>
</html>
Explanation:
html
<
lang="en">
<head>
m
< eta
charset="UTF-8">
<m
eta
ame=" viewport"
n
content="width=device-width, initial-scale=1.0">
<t itle>FrameStructure</title>
/head>
<
<body>
<h
1>Frame Structure</h1>
framename="Frame 2">
<
<framename="Frame 3">
<framename="Frame 4">
</frameset>
<s
cript>
functionloadPage(frameName, pageUrl) {
document.frames[frameName].location.href = pageUrl;
}
window.onload =function() {
// Add click event listeners to the buttons in Frame 1
ocument.querySelector('#Frame 1
d
button[name="SPORT"]').addEventListener('click',function() {
loadPage('Frame 2','sport.html');
});
document.querySelector('#Frame 1
button[name="MUSIC"]').addEventListener('click',function() {
loadPage('Frame 3','music.html');
});
document.querySelector('#Frame 1
button[name="DANCE"]').addEventListener('click',function() {
loadPage('Frame 4','dance.html');
});
};
</script>
/body>
<
</html>
Explanation:
Example usage:
1. Save the above code as index.html and place the sport.html, music.html,
and dance.html files in the same directory.
2. Open the index.html file in a web browser.
3. Click on theSPORTbutton in Frame 1. You should see the sport.html page
appear in Frame 2.
4. Click on theMUSICbutton in Frame 1. You should see the music.html page
appear in Frame 3.
5. Click on theDANCEbutton in Frame 1. You should see the dance.html page
appear in Frame 4.