Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6694077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:03:37+00:00 2026-05-26T06:03:37+00:00

I wonder if you can help me. I am using the below code and

  • 0

I wonder if you can help me.

I am using the below code and wanted to know if anyone know how to get the function in the JS to work independently on UL’s rather interfering with each other.

Basically when the page loads it calls the JS function which checks the inputs on the form for the checked radio button and applies a specific CSS class to set styles and highlight to it’s parent LI in the HTML mark-up. It also works out from the click event if you choose another option in the radio button list to remove any styles that may have been applied to any LI’s and put the class on the corresponding parent LI of the option you have clicked on

Now the problem lies is that as I am checking it against a form (which houses the UL and children LI’s that contain the radio button list) where there are two different radio button lists which breaks the code as it doesn’t know which UL in the form to apply the code to. It works fine if I only have one UL so really I need the JS to be a bit more ‘cleverer’ and run on the corresponding UL that I have clicked on and not affect the other. I still want it to highlight the checked radio button on both lists on page load though

Would greatly appreciate any help.

HTML:

<form id="myForm">
   <ul id="activityChoices" class="radioList">
      <li class="radio-item">
          <input type="radio" id="radio1" name="radio" value="radio1">
          <label for="radio1">radio1</label>
      </li>
      <li class="radio-item">
          <input type="radio" id="radio2" name="radio" value="radio2">
          <label for="radio2">radio2</label>
      </li>
      <li class="radio-item">
          <input type="radio" id="radio3" name="radio" value="radio3" checked="checked">
          <label for="radio3" >radio3</label>
      </li>
      <li class="radio-item">
          <input type="radio" id="radio4" name="radio" value="radio4">
          <label for="radio4" >radio4</label>
      </li>
  </ul>
  <ul>
      <li>
          <input type="radio" id="yes" name="answer" value="yes">
          <label for="yes">yes</label>
      </li>
      <li>
          <input type="radio" id="no" name="answer" value="no">
          <label for="no">no</label>
      </li>
  </ul>
</form>
<script type="text/javascript">
    highlightSelectedRadio('myForm','input');
</script>

JS:

function highlightSelectedRadio(myForm, tag) 
{
  var form = document.getElementById(myForm);
  var inputArray = form.getElementsByTagName(tag);
  var prevClass = null;

  for (i=0;i<inputArray.length;i++) {
    if(inputArray[i].checked) {
      prevClass = inputArray[i].parentNode.getAttribute("class");
      inputArray[i].parentNode.className += " myclass";
    }   
  }

  var myArray = form.getElementsByClassName("myclass");
  form.onclick = function() {
    for (j=0;j<myArray.length;j++) {
      myArray[j].className = prevClass;
    }
    inputArray = this.getElementsByTagName(tag);

    for (i=0;i<inputArray.length;i++) {
      if(inputArray[i].checked) {
        inputArray[i].parentNode.className += " myclass";
      }   
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:03:37+00:00Added an answer on May 26, 2026 at 6:03 am

    If I understood your question correctly, I think this is what you need:

    function highlightSelectedRadio(myForm, tag) 
    {
        var form = document.getElementById(myForm);
        var radios = form.getElementsByTagName(tag);
    
        for (var i=0, l = radios.length;i < l; i++) {
            if (radios[i].checked) {
                addClass(radios[i].parentNode, 'myclass');
            }
        }
    
        form.onclick = function() {
            for (var i = 0, l = radios.length; i < l; i++) {
                if (radios[i].checked) {
                    addClass(radios[i].parentNode, 'myclass');
                } else {
                    removeClass(radios[i].parentNode, 'myclass');
                }
            }
        };
    }
    
    function addClass(el, c) {
        if (el.className.indexOf(c) === -1) {
            el.className += ' myclass';
        }
    }
    function removeClass(el, c) {
        var re = new RegExp(' ?' + c + ' ?');
        el.className = el.className.replace(re, '');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder if anyone can help me. I'm using PodHawk - a basic podcast
I wonder if someone can help: Long story short, I'm using MSSQL2005 to build
Wonder if anyone can help me understand how to sum up the column of
I wonder if anyone can help improve my understanding of JOINs in SQL. [If
I wonder if anyone can help - I have this error showing recently when
I wonder if anyone can help me to understand where I could be going
I wonder if anyone can help with a jQuery problem I am having. I
I'm a newbie programmer working with jQuery and wonder if anyone can help me
I wonder if anyone can help, i wish to save a variable globally for
I wonder to know if someone can help to understand if I'm doing well

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.