I have a list that looks like that, so the user should check the item one by one and get a feedback the item is checked or not.
<html>
<head>
<link href="../../fuctions/style.css" rel="stylesheet" media="screen" type="text/css" />
<script type="text/javascript" charset="utf-8" src="../../fuctions/cordova-2.3.0.js"></script>
<script type="text/javascript" charset="utf-8" src="../../fuctions/functions.js"></script>
</head>
<body>
<div id="topbar">
<div id="title">
Engine Run-up
</div>
<div id="leftnav">
Home
Overview
</div>
<div id="rightnav">
Next
</div>
</div>
<div id="content">
<ul class="pageitem">
<li class="radiobutton">
<span class="name">Toe Brakes -<font color="red"> hold</font></span>
<input name="1" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Safety Belts -<font color="red"> fastened</font></span>
<input name="2" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Canopy -<font color="red"> closed / locked</font></span>
<input name="3" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Fuel Pressure Warning Light -<font color="red"> OFF</font></span>
<input name="4" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Fuel Shut-off Valve -<font color="red"> check OPEN</font></span>
<input name="5" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Fuel Quantity Indicator -<font color="red"> check</font></span>
<input name="6" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Engine Gauges -<font color="red"> green range</font></span>
<input name="7" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Trim -<font color="red"> NEUTRAL</font></span>
<input name="8" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Controls -<font color="red"> free</font></span>
<input name="9" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Throttle -<font color="red"> 1700-1800 RPM</font></span>
<input name="10" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Prop Control Level -<font color="red"> Cycle 3 x (50-250 RPM)</font></span>
<input name="11" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Ignition Switch - Cycle -<font color="red"> L-BOTH-R-BOTH</font></span>
<input name="12" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Throttle -<font color="red"> 1500 RPM</font></span>
<input name="13" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Carburetor Heat -<font color="red"> ON (<50 RPM)</font></span>
<input name="14" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Throttle -<font color="red"> IDLE</font></span>
<input name="15" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Carniretor Heat -<font color="red"> OFF</font></span>
<input name="16" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Corcuit Breakers -<font color="red"> check IN</font></span>
<input name="17" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Electric Fuel Pump -<font color="red"> ON</font></span>
<input name="18" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Wing Flaps -<font color="red"> T/O</font></span>
<input name="19" type="radio" value="other" />
</li>
<li class="radiobutton">
<span class="name">Parking Brake -<font color="red"> release</font></span>
<input name="20" type="radio" value="other" />
</li>
</ul>
</div>
<div id="footer">
<a class="noeffect" href="#" onClick="resetChecklist();">Reset ZYX</a>
<br /><br />
<a class="noeffect" href="#" onClick="openXYZDE();">XYZ</a>
</div>
</body>
I want to check via JavaScript whether one of the "li" is checked, if so it should change that line to "green".
<li class="radiobutton">
<span class="name">Toe Brakes -<font color="green"> hold</font></span>
<input name="1" type="radio" value="other" />
</li>
If jquery is an option, it becomes pretty simple.
function checkChanges()
{
$('.radiobutton font').attr('color', 'red');
$('.radiobutton :checked').closest('.radiobutton').find('font').attr('color', 'green');
}
$(function()
{
checkChanges();
$('.radiobutton :radio').on('click', checkChanges);
});
Although I HIGHLY recommend removing the font tag and instead add a class to the li, such as radiobutton-checked which sets the font color to green.
The ab ove code will reset all fonts to red, then find the one radio button with a checked radio inside it, iterate back up to the parent radiobutton (closest) and then find its child font tag and set its color to green. Then anytime a radio button gets clicked, it repeats the above process, thus keeping only the actively checked radio button green.
Related
I am currently new to web development, how do you get the total value of the chosen radio buttons and store it in an array then the link or page would be different base on the total value. Is it possible in javascript? let's say that the total values of the selected buttons is 50 then it will go to page1 if not it will go to page2 and so on.
<HTML>
<head>
<link rel="stylesheet" type="text/css" href="css.css">
</head>
<div class="wrap">
<h1 class="likert-header">Likert Scale survey</h1>
<form action="">
<label class="statement">Q1.</label>
<ul class='likert'>
<li>
<input type="radio" name="likert" value="25">
<label>Strongly agree</label>
</li>
<li>
<input type="radio" name="likert" value="25">
<label>Agree</label>
</li>
<li>
<input type="radio" name="likert" value="strong_agree">
<label>Neutral</label>
</li>
<li>
<input type="radio" name="likert" value="-25">
<label>Disagree</label>
</li>
<li>
<input type="radio" name="likert" value="-25">
<label>Strongly disagree</label>
</li>
</ul>
<label class="statement">Q2.</label>
<ul class='likert'>
<li>
<input type="radio" name="likert" value="25">
<label>Strongly agree</label>
</li>
<li>
<input type="radio" name="likert" value="25">
<label>Agree</label>
</li>
<li>
<input type="radio" name="likert" value="strong_agree">
<label>Neutral</label>
</li>
<li>
<input type="radio" name="likert" value="-25">
<label>Disagree</label>
</li>
<li>
<input type="radio" name="likert" value="-25">
<label>Strongly disagree</label>
</li>
</ul>
<button class="clear">Clear</button>
<button class="submit">Submit</button>
</div>
</form>
</div>
</HTML>
I have the following html and I want to set the value of the second radio button in the list to be checked. I know I can do this using the id attribute but how can I do the same
by using the class selector as below and chaining the input field to be checked?
<ul class="myOptionList">
<li>
<input id="1" type="radio" value="1" name="options">
<label >one</label>
</li>
<li>
<input id="2" type="radio" value="2" name="options">
<label >two</label>
</li>
...
</ul>
$(".myOptionList li:nth-child(2)"); // how do i chain input field and set it as checked
Below code may help you.
HTML
<ul class="myOptionList">
<li>
<input id="1" type="radio" value="1" name="options">
<label >one</label>
</li>
<li>
<input id="2" type="radio" value="2" name="options">
<label >two</label>
</li>
jQuery
$(".myOptionList li:nth-child(2)").find('input:radio').prop('checked',true);
For versions of jQuery prior to 1.6, use:
$(".myOptionList li:nth-child(2)").find('input:radio').attr('checked', 'checked');
$(document).ready(function(){
$(".radio1 li:nth-child(4)").find('input:radio').prop('checked',true);
$(".radio2 li:nth-child(2)").find('input:radio').prop('checked',true);
$(".radio li:nth-child(1)").find('input:radio').prop('checked',true);
});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
Fourth child selected:
<ul class="radio1">
<li>
<input id="1" type="radio" value="1" name="option1">
<label >one</label>
</li>
<li>
<input id="2" type="radio" value="2" name="option1">
<label >two</label>
</li>
<li>
<input id="3" type="radio" value="3" name="option1">
<label >three</label>
</li>
<li>
<input id="4" type="radio" value="4" name="option1">
<label >four</label>
</li>
</ul>
Second child selected:
<ul class="radio2">
<li>
<input id="1" type="radio" value="1" name="option2">
<label >one</label>
</li>
<li>
<input id="2" type="radio" value="2" name="option2">
<label >two</label>
</li>
<li>
<input id="3" type="radio" value="3" name="option2">
<label >three</label>
</li>
<li>
<input id="4" type="radio" value="4" name="option2">
<label >four</label>
</li>
</ul>
First child selected:
<ul class="radio">
<li>
<input id="1" type="radio" value="1" name="option">
<label >one</label>
</li>
<li>
<input id="2" type="radio" value="2" name="option">
<label >two</label>
</li>
</ul>
First child selected:(same class name)
<ul class="radio">
<li>
<input id="1" type="radio" value="1" name="option4">
<label >one</label>
</li>
<li>
<input id="2" type="radio" value="2" name="option4">
<label >two</label>
</li>
</ul>
</body>
</html>
Even the use of:
$(".myOptionList li:nth-child(2)").find('input:radio').prop('checked',true);
Works apparently, it is just for a case.
It set the checked on all the radio until the last one, that it is set just because it is the last.
A more meaning solution would be to use the .last() to go directly on the radio option you want to set:
$(function(){
// Set the last checked radio
$('.myOptionList li > input[name="options"]').last().prop('checked', true)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="myOptionList">
<li>
<input id="1" type="radio" value="1" checked name="options">
<label >one</label>
</li>
<li>
<input id="2" type="radio" value="2" name="options">
<label >two</label>
</li>
...
</ul>
To ensure it's working properly I set the first radio checked.
But I suggest to change your code in a way that you select the radio on the value attribute.
The benefit of this is that your code will be more clear and easy to understand and change, and more if the order of radios change it will still works.
I have following HTML with two ULs.
Click
<ul class="plan none">
<li>
<p>One month</p>
<label for="2" class="label_radio">
<input type="radio" value="40" id="2" data-name="Be Bold" name="be_bold">
</label> <strong>40 ₪</strong>
</li>
<li>
<p>Three months</p>
<label for="3" class="label_radio">
<input type="radio" value="70" id="3" data-name="Be Bold" name="be_bold" checked="checked">
</label> <strong>70 ₪</strong>
</li>
<li>
<p>Six months</p>
<label for="4" class="label_radio">
<input type="radio" value="100" id="4" data-name="Be Bold" name="be_bold">
</label> <strong>100 ₪</strong>
</li>
</ul>
<hr />
<ul class="plan">
<li>
<p>One month</p>
<label for="2" class="label_radio">
<input type="radio" value="40" id="2" data-name="Be Bold" name="be_bold_two">
</label> <strong>40 ₪</strong>
</li>
<li>
<p>Three months</p>
<label for="3" class="label_radio">
<input type="radio" value="70" id="3" data-name="Be Bold" name="be_bold_two">
</label> <strong>70 ₪</strong>
</li>
<li>
<p>Six months</p>
<label for="4" class="label_radio">
<input type="radio" value="100" id="4" data-name="Be Bold" name="be_bold_two">
</label> <strong>100 ₪</strong>
</li>
</ul>
First UL is with display: none and have one radio button is selected by default and Second UL is without selected.
Now I want get selected radio button value from visible UL.
Here is my jQuery Code:
$("#detail_buttons").click(function () {
if ($(".plan input[type='radio']:checked").val() != undefined) {
alert($(".plan input[type='radio']:checked").val());
}
});
My JSFiddle: Sample
Any Idea?
Thanks.
Use this way:
$(".plan:visible").find("input:radio:checked").val();
Snippet
$("#detail_buttons").click(function () {
alert($(".plan:visible").find("input:radio:checked").val());
return false;
});
.none {display: none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Check Value
<ul class="plan none">
<li>
<p>One month</p>
<label for="2" class="label_radio">
<input type="radio" value="40" id="2" data-name="Be Bold" name="be_bold">
</label> <strong>40 ₪</strong>
</li>
<li>
<p>Three months</p>
<label for="3" class="label_radio">
<input type="radio" value="70" id="3" data-name="Be Bold" name="be_bold" checked="checked">
</label> <strong>70 ₪</strong>
</li>
<li>
<p>Six months</p>
<label for="4" class="label_radio">
<input type="radio" value="100" id="4" data-name="Be Bold" name="be_bold">
</label> <strong>100 ₪</strong>
</li>
</ul>
<hr />
<ul class="plan">
<li>
<p>One month</p>
<label for="2" class="label_radio">
<input type="radio" value="40" id="2" data-name="Be Bold" name="be_bold_two">
</label> <strong>40 ₪</strong>
</li>
<li>
<p>Three months</p>
<label for="3" class="label_radio">
<input type="radio" value="70" id="3" data-name="Be Bold" name="be_bold_two">
</label> <strong>70 ₪</strong>
</li>
<li>
<p>Six months</p>
<label for="4" class="label_radio">
<input type="radio" value="100" id="4" data-name="Be Bold" name="be_bold_two">
</label> <strong>100 ₪</strong>
</li>
</ul>
So here is the code
<html>
<head>
<link href="../../fuctions/style.css" rel="stylesheet" media="screen" type="text/css" />
<script type="text/javascript" charset="utf-8" src="../../fuctions/cordova-2.3.0.js"></script>
<script type="text/javascript" charset="utf-8" src="../../fuctions/functions.js"></script>
<script type="text/javascript" charset="utf-8" src="../../fuctions/jq.js"></script>
</head>
<body>
<div id="topbar">
<div id="title"><!--Title-->
After Landing
</div>
<div id="leftnav">
Home
Overview<!--AllChecklist-->
</div>
<div id="rightnav">
Next</div><!--NextPage-->
</div>
<div id="content">
<ul class="pageitem">
<li class="radiobutton" onClick="checkChanges()">
<span class="name">Wing Flaps -<font color="red"> UP
</font></span>
<input name="1" type="radio" value="other" />
</li><!--CheckpunktEnd-->
<li class="radiobutton" onClick="checkChanges()">
<span class="name">Carburetor Heat -<font color="red"> OFF
</font></span>
<input name="2" type="radio" value="other" />
</li><!--CheckpunktEnd-->
<li class="radiobutton" onClick="checkChanges()">
<span class="name">Exterior Lights -<font color="red"> as required
</font></span>
<input name="3" type="radio" value="other" />
</li><!--CheckpunktEnd-->
<li class="radiobutton" onClick="checkChanges()">
<span class="name">Electric Fuel Pump -<font color="red"> OFF
</font></span>
<input name="4" type="radio" value="other" />
</li><!--CheckpunktEnd-->
</ul>
</div>
<div id="footer">
<a class="noeffect" href="#" onClick="resetChecklist();">Reset Checklist</a><!--ResetChecklist-->
<br /><br />
<a class="noeffect" href="#" onClick="openxyzDE();">xyz</a>
</div>
</body>
</html>
And i got a function that check for "radio is checked and change colour"
function checkChanges()
{
$('.radiobutton font').attr('color', 'red');
$('.radiobutton :checked').closest('.radiobutton').find('font').attr('color', 'green');
}
$(function()
{
checkChanges();
$('.radiobutton :radio').on('click', checkChanges);
});
I want to add to that code a function that uncheck the Radio if its checked and been clicked again (onClick) this should also change the colour back to red.
Few corrections...
Remove the space before the :checked. Change:
$('.radiobutton :checked').closest('.radiobutton').find('font').attr('color', 'green');
To:
$('.radiobutton:checked').closest('.radiobutton').find('font').attr('color', 'green');
You need to use input[type="radio"] here. Change:
$('.radiobutton :radio').on('click', checkChanges);
To:
$('input.radiobutton[type="radio"]').on('click', checkChanges);
On clicking on the Populate Values button the div below the button should populate the values of the selected radio buttons of the preferred and home locations respectively
I want to do it unobtrusively without inline JavaScript
<div id="form_block">
<div class="home-location">
<ul>
<li>Home Location</li>
<li>
<input type="radio" name="home-location" value="india" class="radio" id="home-india" /><label
for="home-india">India</label></li>
<li>
<input type="radio" name="home-location" value="usa" class="radio" id="home-usa" /><label
for="home-usa">USA</label></li>
</ul>
</div>
<div class="prefer-location">
<ul>
<li>Preferred Location</li>
<li>
<input type="radio" name="home-preferred" value="india" class="radio" id="preferred-india" /><label
for="preferred-india">India</label></li>
<li>
<input type="radio" name="home-preferred" value="usa" class="radio" id="preferred-usa" /><label
for="preferred-usa">USA</label></li>
</ul>
</div>
<div class="result">
My Home Location is: <span class="home-location-result"></span>and my preferred
location is: <span class="home-location-result"></span>
</div>
<input type="submit" value="Populate Values" id="ss" class="submit" />
Modified your HTML. Provided an id attribute for your result spans
$(function(){
$("#ss").click(function(){
// Find all input elements with type radio which are descendants of element with id `form`
var filt = $("#form_block input:radio");
// Apply a filter to the above object with `name='home-location'` and checked and get the value
var homeVal = filt.filter("[name='home-location']:checked").val();
// same as above
var preferredVal = filt.filter("[name='home-preferred']:checked").val();
// Set the text of the element width id `home-location-result' with the computed value
$("#home-location-result").text(homeVal);
// same as above
$("#preferred-location-result").text(preferredVal);
return false;
});
});
See a working demo
use this it's works for me
<script>
function test()
{
var home= $('input:radio[name=home-location]:checked').val();
document.getElementById("h1").innerHTML=home;
var preferred= $('input:radio[name=home-preferred]:checked').val();
document.getElementById("h2").innerHTML=preferred;
return false;
}
</script>
<form onsubmit="return test();">
<div id="form_block">
<div class="home-location">
<ul>
<li>Home Location</li>
<li>
<input type="radio" name="home-location" value="india" class="radio" id="home-india" /><label
for="home-india">India</label></li>
<li>
<input type="radio" name="home-location" value="usa" class="radio" id="home-usa" /><label
for="home-usa">USA</label></li>
</ul>
</div>
<div class="prefer-location">
<ul>
<li>Preferred Location</li>
<li>
<input type="radio" name="home-preferred" value="india" class="radio" id="preferred-india" /><label
for="preferred-india">India</label></li>
<li>
<input type="radio" name="home-preferred" value="usa" class="radio" id="preferred-usa" /><label
for="preferred-usa">USA</label></li>
</ul>
</div>
<div class="result">
My Home Location is: <span class="home-location-result" id="h1"></span>and my preferred
location is: <span class="home-location-result" id='h2'></span>
</div>
<input type="submit" value="Populate Values" id="ss" class="submit" />
</form>