how to group the radio button in html with javascript - html

One div should select only one radio button and if one div selects test radio than other div should select taining radio.
<div><label>Test1<input type="radio" value="test" name="test" /></label>
<label>Training<input type="radio" value="training" name="training" /></label>
</div><hr />
<div>
<label>Test<input type="radio" value="test" name="test" /></label>
<label>Training<input type="radio" value="training" name="training" /></label>
</div>
here is the fiddle

Is this what you were looking for?
var rad = document.myForm1.myRadios;
var rad2 = document.myForm2.myRadios;
for(var i = 0; i < rad.length; i++) {
rad[i].onclick = function() {
if(this.value==1){
document.getElementById('f2v2').checked = true;
} else {
document.getElementById('f2v1').checked = true;
}
}
}
for(var i = 0; i < rad2.length; i++) {
rad2[i].onclick = function() {
if(this.value==1){
document.getElementById('f1v2').checked = true;
} else {
document.getElementById('f1v1').checked = true;
}
}
}
<form name="myForm1">
Testing<input type="radio" name="myRadios" value="1" id="f1v1"/>
Training<input type="radio" name="myRadios" value="2" id="f1v2"/>
</form>
<form name="myForm2">
Testing<input type="radio" name="myRadios" value="1" id="f2v1"/>
Training<input type="radio" name="myRadios" value="2" id="f2v2"/>
</form>

Related

get value from checkbox but not the word from checkbox

I have two checkbox and under this when tick one of two checkbox I have two another option checkbox and another tick there will be text. The text will be value from the checkbox, how can I get the value from this checkbox? But the text I want to put are different sentence for each option checkbox selected not the word from checkbox. Did anyone know?
And my another problem is the checkbox not working if I do not tick the first checkbox.
<script type="text/javascript">
function ShowHideDiv(Cat) {
var dvCat = document.getElementById("bigCat");
bigCat.style.display = Cat.checked ? "block" : "none";
}
</script>
<label for="Cat">
<input type="checkbox" id="Cat" onclick="ShowHideDiv(this)" /> Cat
</label>
<script type="text/javascript">
function ShowHideDiv2(Rabbit) {
var bigRabbit = document.getElementById("bigRabbit");
bigRabbit.style.display = Rabbit.checked ? "block" : "none";
}
</script>
<label for="Rabbit">
<input type="checkbox" id="Rabbit" onclick="ShowHideDiv2(this)" /> Rabbit
</label>
<div id="bigCat" style="display: none">
<label>
<input type="checkbox" id="bigSubCat" /> British shorthair
</label>
<label>
<input type="checkbox" id="bigSubCat" /> Exotic Shorthair
</label>
<div id="bigRabbit" style="display: none">
<label>
<input type="checkbox" id="bigSubRabbit" /> White Rabbit
</label>
<label>
<input type="checkbox" id="bigSubRabbit" /> Black Rabbit
</label>
Add value attribute to the checkbox. and set value to the checkbox.Or you can take the text from the label
<script type="text/javascript">
function ShowHideDiv(Cat) {
var dvCat = document.getElementById("bigCat");
bigCat.style.display = Cat.checked ? "block" : "none";
console.log(Cat.value)
console.log("Text Inside LABEL:" + Cat.parentNode.textContent )
}
</script>
<label for="Cat">
<input type="checkbox" id="Cat" onclick="ShowHideDiv(this)" value="cat"/> Cat
</label>
<script type="text/javascript">
function ShowHideDiv2(Rabbit) {
var bigRabbit = document.getElementById("bigRabbit");
bigRabbit.style.display = Rabbit.checked ? "block" : "none";
console.log(Rabbit.value)
console.log("Text Inside LABEL:" + Rabbit.parentNode.textContent )
}
</script>
<label for="Rabbit">
<input type="checkbox" id="Rabbit" onclick="ShowHideDiv2(this)" value="Rabbit"/> Rabbit
</label>
<div id="bigCat" style="display: none">
<label>
<input type="checkbox" id="bigSubCat" /> British shorthair
</label>
<label>
<input type="checkbox" id="bigSubCat" /> Exotic Shorthair
</label>
</div>
<div id="bigRabbit" style="display: none">
<label>
<input type="checkbox" id="bigSubRabbit" /> White Rabbit
</label>
<label>
<input type="checkbox" id="bigSubRabbit" /> Black Rabbit
</label>
I think you can follow this simple logic:
https://jsfiddle.net/pablodarde/6p7zkfwf/
HTML
<div class="container">
<div class="row">
<input type="checkbox" value="This is option one!" id="opt1"><label for="opt1">Option One</label>
<p></p>
</div>
<div class="row">
<input type="checkbox" value="This is option two!" id="opt2"><label for="opt2">Option Two</label>
<p></p>
</div>
</div>
JavaScript
const checkboxes = document.querySelectorAll('.container input[type=checkbox]');
for (let i = 0, l = checkboxes.length; i < l; i++) {
checkboxes[i].addEventListener('click', (e) => {
if(checkboxes[i].parentNode.querySelector('p').innerHTML == "") {
checkboxes[i].parentNode.querySelector('p').innerHTML = checkboxes[i].value;
} else {
checkboxes[i].parentNode.querySelector('p').innerHTML = "";
}
});
}
CSS
.row {
padding-bottom: 10px;
border-bottom: 1px solid black;
}

Multiple Choice Quiz with Scoring and image output

So I am having issues creating a scoring system that will take my users inpiut and place them into a category of a fake fan, hardcore fan, etc.
I had to repost this question, because it was closed. hopefully this post is more clear
this is for a project that is due tonight, and i am completely lost. these are the requirements:
"Take in and store user input from ten different questions answered by the user via HTML form elements
Use an array of options to correlate user data to a matrix of potential results
Dynamically display appropriate result images and text to the user after completing the questions
Format appropriately for use on a mobile, tablet, or desktop screen using media queries"
this is the code i have so far
<html>
<head>
<title>Demi Quiz</title>
</head>
<style>
body {
background-color: #ffffff;
}
.col {
color: #fff;
float: left;
margin: 2%;
width: 46%;
}
.one {
background-color: black;
}
.two {
background-color: black;
}
.three {
background-color: black;
}
.four {
background-color: black;
}
.five {
background-color: black;
}
.six {
background-color: black;
}
.seven {
background-color: black;
}
.eight {
background-color: black;
}
.nine {
background-color: black;
}
.ten {
background-color: black;
}
#media screen and (max-width: 600px) {
.col {
float: none;
margin: 0;
width: 100%;
}
h1 {
color: red;
text-align: center;
}
</style>
<body>
<form>
<h1>How Big Of A Demi Lovato Fan Are You?<h1>
<div class="col one">
<br>
1. Who did Demi punch back in 2010?
<br>
<input type="radio" name="one" id="manager" value="Wannabe"> <label for="manager">Manager</label>
<br><input type="radio" name="one" id="costar" value="Lowkey"> <label for="costar">CoStar</label>
<br><input type="radio" name="one" id="father" value="Fake"> <label for="father">Father</label>
<br><input type="radio" name="one" id="dancer" value="Hardcore"> <label for="dancer">Dancer</label>
</div>
<div class="col two">
<br>
2. What song did Demi sing on AS THE BELL RINGS?
<br>
<input type="radio" name="two" id="heartattack" value="Wannabe"> <label for="heartattack">HeartAttack</label>
<br><input type="radio" name="two" id="together" value="Fake"> <label for="together">Together</label>
<br><input type="radio" name="two" id="shadow" value="Hardcore"> <label for="shadow">Shadow</label>
<br><input type="radio" name="two" id="gotta" value="Lowkey"> <label for="gotta">Gotta Find You</label>
</div>
<div class="col three">
<br>
3. How did Demi celebrate her 21st birthday?
<br>
<input type="radio" name="three" id="partying" value="Fake"> <label for="partying">Partying</label>
<br><input type="radio" name="three" id="africa" value="Hardcore"> <label for="africa">Mission Trip to Africa</label>
<br><input type="radio" name="three" id="disneyland" value="Lowkey"> <label for="disneyland">Disneyland</label>
<br><input type="radio" name="three" id="home" value="Wannabe"> <label for="home">At Home</label>
</div>
<div class="col four">
<br>
4. What celebrity sent Demi flowers when she was in Rehab?
<br>
<input type="radio" name="four" id="selena" value="Lowkey"> <label for="selena">Selena</label>
<br><input type="radio" name="four" id="miley" value="Hardcore"> <label for="miley">Miley</label>
<br><input type="radio" name="four" id="taylor" value="Wannabe"> <label for="taylor">Taylor</labe>
<br><input type="radio" name="four" id="ashley" value="Fake"> <label for="ashley">Ashley</label>
</div>
<div class="col five">
<br>
5. What substance abuse addictions did Demi struggle with?
<br>
<input type="radio" name="five" id="alcohol" value="Wannabe"> <label for="alcohol">Alcohol</label>
<br><input type="radio" name="five" id="cocaine" value="Lowkey"> <label for="cocaine">Cocaine</label>
<br><input type="radio" name="five" id="weed" value="Fake"> <label for="weed">Weed</label>
<br><input type="radio" name="five" id="all" value="Hardcore"> <label for="all">All Of The Above</label>
</div>
<div class="col six">
<br>
6. What artist has Demi NOT collaborated with?
<br>
<input type="radio" name="six" id="adele" value="Hardcore"> <label for="slytherin">Adele</label>
<br><input type="radio" name="six" id="miguel" value="Lowkey"> <label for="miguel">Miguel</label>
<br><input type="radio" name="six" id="missy" value="Fake"> <label for="missy">Missy Elliot</label>
<br><input type="radio" name="six" id="john" value="Wannabe"> <label for="john">John Mayer</label>
</div>
<div class="col seven">
<br>
7. What disease does Demi Lovato struggle with?
<br>
<input type="radio" name="seven" id="lupus" value="Lowkey"> <label for="lupus">Lupus</label>
<br><input type="radio" name="seven" id="diabetes" value="Wannabe"> <label for="diabetes">Diabetes</label>
<br><input type="radio" name="seven" id="bipolar" value="Hardcore"> <label for="bipolar">Bipolar</label>
<br><input type="radio" name="seven" id="none" value="Fake"> <label for="none">None</label>
</div>
<div class="col eight">
<br>
8. What kind of shows does Demi enjoy watching?
<br>
<input type="radio" name="eight" id="reality" value="Fake"> <label for="reality">Reality Shows</label>
<br><input type="radio" name="eight" id="crime" value="Hardcore"> <label for="crime">Crime Shows</label>
<br><input type="radio" name="eight" id="sitcoms" value="Lowkey"> <label for="sitcoms">Sitcoms</label>
<br><input type="radio" name="eight" id="game" value="Wannabe"> <label for="game">Game Shows</label>
</div>
<div class="col nine">
<br>
9. Which artist did Demi shade in 2016?
<br>
<input type="radio" name="nine" id="taylor" value="Lowkey"> <label for="taylor">Taylor Swift</label>
<br><input type="radio" name="nine" id="nicki" value="Wannabe"> <label for="nicki">Nicki Minaj</label>
<br><input type="radio" name="nine" id="selena" value="Fake"> <label for="selena">Selena Gomez</label>
<br><input type="radio" name="nine" id="all" value="Hardcore"> <label for="all">All of the Above</label>
</div>
<div class="col ten">
<br>
10. Which celebrity friendship has Demi NOT publicly made?
<br>
<input type="radio" name="ten" id="jennifer" value="Fake"> <label for="jennifer">Jennifer Lopez</label>
<br><input type="radio" name="ten" id="ariana" value="Lowkey"> <label for="ariana">Ariana Grande</label>
<br><input type="radio" name="ten" id="christina" value="Hardcore"> <label for="christina">Christina Aguilera</label>
<br><input type="radio" name="ten" id="iggy" value="Wannabe"> <label for="iggy">Iggy Azalea</label>
</div>
<br><input type="button" id="quizButton" value="Get Results!"><br>
</form>
<p id="oneParagraph"></p>
</body>
<script>
//set up a function to run when the window is loaded
//grab the button and wait for a click
function init() {
var button = document.getElementById('quizButton');
button.onclick = checkQuiz;
}
//get the results when the button is clicked
function checkQuiz() {
//confirm that the button was clicked
//alert('Button was clicked!');
//create a variable to store the user's house
var one;
//get the input data as an array
var inputs = document.getElementsByName('one');
//loop through all the possible inputs (the radio buttons)
for (var i=0; i < inputs.length; i++) {
//see if one of the buttons is selected
if (inputs[i].checked) {
//if it is save the house name
one = inputs[i].value;
alert(one);
displayOne(one);
/*
var two;
//get the input data as an array
var inputs = document.getElementsByName('two');
//loop through all the possible inputs (the radio buttons)
for (var i=0; i < inputs.length; i++) {
//see if one of the buttons is selected
if (inputs[i].checked) {
//if it is save the house name
two = inputs[i].value;
alert(two);
displayTwo(two);
var three;
//get the input data as an array
var inputs = document.getElementsByName('three');
//loop through all the possible inputs (the radio buttons)
for (var i=0; i < inputs.length; i++) {
//see if one of the buttons is selected
if (inputs[i].checked) {
//if it is save the house name
three = inputs[i].value;
alert(three);
displayThree(three);
var four;
//get the input data as an array
var inputs = document.getElementsByName('four');
//loop through all the possible inputs (the radio buttons)
for (var i=0; i < inputs.length; i++) {
//see if one of the buttons is selected
if (inputs[i].checked) {
//if it is save the house name
four = inputs[i].value;
alert(four);
displayFour(four);
var five;
//get the input data as an array
var inputs = document.getElementsByName('five');
//loop through all the possible inputs (the radio buttons)
for (var i=0; i < inputs.length; i++) {
//see if one of the buttons is selected
if (inputs[i].checked) {
//if it is save the house name
five = inputs[i].value;
alert(five);
displayFive(five);
var six;
//get the input data as an array
var inputs = document.getElementsByName('six');
//loop through all the possible inputs (the radio buttons)
for (var i=0; i < inputs.length; i++) {
//see if one of the buttons is selected
if (inputs[i].checked) {
//if it is save the house name
six = inputs[i].value;
alert(six);
displaySix(six);
var seven;
//get the input data as an array
var inputs = document.getElementsByName('seven');
//loop through all the possible inputs (the radio buttons)
for (var i=0; i < inputs.length; i++) {
//see if one of the buttons is selected
if (inputs[i].checked) {
//if it is save the house name
seven = inputs[i].value;
alert(seven);
displaySeven(seven);
var eight;
//get the input data as an array
var inputs = document.getElementsByName('eight');
//loop through all the possible inputs (the radio buttons)
for (var i=0; i < inputs.length; i++) {
//see if one of the buttons is selected
if (inputs[i].checked) {
//if it is save the house name
eight = inputs[i].value;
alert(eight);
displayEight(eight);
var nine;
//get the input data as an array
var inputs = document.getElementsByName('nine');
//loop through all the possible inputs (the radio buttons)
for (var i=0; i < inputs.length; i++) {
//see if one of the buttons is selected
if (inputs[i].checked) {
//if it is save the house name
nine = inputs[i].value;
alert(nine);
displayNine(nine);
var ten;
//get the input data as an array
var inputs = document.getElementsByName('ten');
//loop through all the possible inputs (the radio buttons)
for (var i=0; i < inputs.length; i++) {
//see if one of the buttons is selected
if (inputs[i].checked) {
//if it is save the house name
ten = inputs[i].value;
alert(ten);
displayTen(ten);
*/
}
}
//if house hasn't been defined, no choice was picked
/*if (!one) {
alert('Please answer question one.');
}*/
}
//display a results image for the chosen house
function displayOne(one) {
//make a new image object
var image = document.createElement('img');
//assign it CSS properties with a class
image.className = 'one';
//grab the paragraph to append the image to
var source = document.getElementById('oneParagraph');
// use a \ to indicate a ' in a string
var text = 'You\'re a ' + one + ' Fan!';
//pick the right image source by house using a switch statement
switch(one) {
case 'Fake':
image.src = 'http://24.media.tumblr.com/acf005f7efdd37f9cde57592a80055c5/tumblr_mpqppeDWMY1sqjb9eo3_500.gif';
break;
case 'Wannabe':
image.src = 'http://data3.whicdn.com/images/64130190/large.gif';
break;
case 'Lowkey':
image.src = 'http://24.media.tumblr.com/d4af4200cc8ac0ba5bb0f56afec858dc/tumblr_mj4xhrhsWF1rgmvx3o1_250.gif';
break;
case 'Hardcore':
image.src = 'http://media.giphy.com/media/2VnL5vdmRJfVe/giphy.gif';
break;
default:
image.src = '';
}
//add the image to the paragraph
source.appendChild(image);
//append the text on a new line
source.innerHTML += '<br>' + text;
}
//call the initial function when the window is loaded
window.onload = init;
</script>
<html>
so i think what you have to do is first of all, check if the answer is true or false, you can do that by assigning your javascript for each var (one, two, three) to only 'accept' one answer.
So the way I would do it is check if the answer is correct in the variable for the question. (You can do that by checking the id of the input). Then, if it is correct just say something simple like setting up a variable to equal 1 then whenever they get a correct answer just do:
score += 1;
Then at the end add up all the scores:
if (score <= 3) {fake();}
then for each 'score' (fake, wannabe...), make a function. e.g.
function fake() { image.src = 'http://24.media.tumblr.com/acf005f7efdd37f9cde57592a80055c5/tumblr_mpqppeDWMY1sqjb9eo3_500.gif';}
and then the same for all the others

Get value of selected radio button to Firebase

I have a form:
<form onsubmit="return send(this)">
<div>
<input type="radio" id="1" name="check" value="check1" checked="checked">
<input type="radio" id="2" name="check" value="check2">
<input type="radio" id="3" name="check" value="check3">
</div>
</form>
And js:
function send(formObj) {
myFirebase.push({
checked: formObj.check.value
})
}
How do I get the checked input value and pass it into the object key checked ?
var radiochecked = "";
for(i in formObj.check) {
if(formObj.check[i].checked) {
radiochecked = formObj.check[i].value;
}
}
function send(formObj) {
myFirebase.push({
checked: radiochecked
}

How can I get a solution for this?

I have two radio buttons:
yes and
no
When I select 'yes',it should ask "Enter your requirements" and When I select 'no', nothing should happen.
I have tried this:
<form>
<input type="radio" name="question" value="yes">YES
<input type="radio" name="question" value="no">NO
<input type="button" name="submit" value="submit onclick="getValue('question')">
</form>
<script>
function getvalue(question) {
for (var i = 0; i < question.length; i++) {
var button = question[i];
if (button.checked) {
return button;
}
}
print getValue(question)
HTML
<input type="radio" id="radio1" name="radio" value='Yes' /><label for="radio1">Yes</label>
<input type="radio" id="radio2" name="radio" value='No'/><label for="radio2">No</label>
JS
$(function () {
$("input[name='radio']").on("change", function () {
if($("input[name='radio']:checked").val() == "Yes"){
alert("Enter your requirements");
}
});
});
JSFiddle try this

Selecting radio button in js

I have a problem in my code, I wanna show input text form when I click one of radio button.
Ajax code :
<script type="text/javascript">
$(document).ready(function(){
$('#macam').click( function() {
var value = $(this).val();
if(value == "0"){
$("#hilang").html("<input name='' value='tes' type='text' />");
}
else{
$("#hilang").html("");
}
});
});
</script>
HTML code :
<input id="macam" type="radio" name="radio" value="1" checked></input>
<input id="macam" type="radio" name="radio" value="0"></input>
<div id="hilang"></div>
Don't reuse ids
JS
$(document).ready(function(){
$('input[name="radio"]').click( function() {
var value = $(this).val();
if(value == "0"){
$("#hilang").html("<input name='' value='tes' type='text' />");
}
else{
$("#hilang").html("");
}
});
});
HTML
<input id="macam1" type="radio" name="radio" value="1" checked></input>
<input id="macam0" type="radio" name="radio" value="0"></input>
<div id="hilang"></div>
First problem that jumps out is that you have two identical ID's. Declare your two radios class="macam" instead of id="macam" and use $(".macam") to select those classes instead of $("#.maca")
Use this instead
var value = $('input[name="radio"]:checked').val();
use this
<script type="text/javascript">
$(document).ready(function(){
$('.macam').click( function() {
var value = $('input[name="radio"]:checked').val();
if(value === "0"){
$("#hilang").html("<input name='' value='tes' type='text' />");
}
else{
$("#hilang").html("");
}
});
});
</script>
<input class="macam" type="radio" name="radio" value="1" checked></input>
<input class="macam" type="radio" name="radio" value="0"></input>
<div id="hilang"></div>
Id should be unique. Change it to class.
HTML
<input class="macam" type="radio" name="radio" value="1" checked></input>
<input class="macam" type="radio" name="radio" value="0"></input>
<div id="hilang"></div>
jQuery
$('.macam').click( function() {
var value = $(this).val();
if(value == 0){
$("#hilang").html("<input name='' value='tes' type='text' />");
}
else{
$("#hilang").html("");
}
});