Fabian Boulegue
Fabian Boulegue

Reputation: 6608

HTML List (Li) change via Javascript onClick

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">
            <a href="../../index_aerosoft.html">Home</a>
            <a href="katana_checklist_all.html">Overview</a>
        </div>
        <div id="rightnav">
            <a href="katan_checklist_takeoff.html"id="a_next">Next</a>
        </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>

Upvotes: 1

Views: 1323

Answers (1)

Eli Gassert
Eli Gassert

Reputation: 9763

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.

Upvotes: 1

Related Questions