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 3353596
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:10:14+00:00 2026-05-18T02:10:14+00:00

I am writing a basic web app, this has some list elements that I

  • 0

I am writing a basic web app, this has some list elements that I need to be able to click on the entire LI that surrounds each one to select the radio button held within, and only ensure the one I have clicked is selected. I can do this writing the code within the .click callback, however if I try to call the same code from a named function it falls down, I believe this is something to do with the “this” reference I am using, however I cannot seem to fix it.

The HTML is

<ul> 
  <form id="healthCheckAge" name="healthCheckAge"> 
    <li> <input type="radio" name="eighteen" value="18-30" />18-30 </li> 
    <li> <input type="radio" name="thirtyone" value="31-45" />31-45 </li> 
    <li> <input type="radio" name="fourtysix" value="46-65" />46-65 </li> 
    <li> <input type="radio" name="sixtyfive" value="65+" />65+</li> 
  </form> 
</ul> 

And the JavaScript is

function li_click(){
  //set the vars for the selectors
  var listInputs = $('#healthCheckAge li input');
  var clicked = listInputs.attr('name');
  //loop on all the li children to see if they match the clicked LI - if not ensure that they are unchecked and faded
  $(listInputs).each(function(index,Element){
    $(listInputs).attr('checked',false).parent().addClass('selected');
  });
  //set the radio button to checked and apply a style to it, while removing this from any other checked radio buttoon
  if($(listInputs).attr('name')==clicked){
    $(this).children().attr('checked','true').parent().removeClass('selected'); 
  } 
}

$('#healthCheckAge li').click(function(){
  li_click();                                 
});

I think the problem is that when I call the very last conditional statement the ‘this’ is not reflecting what I need.

Any ideas?

  • 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-18T02:10:14+00:00Added an answer on May 18, 2026 at 2:10 am

    Just pass the element to the function:

    function lol(element){
      //set the vars for the selectors
      var listInputs = $('#healthCheckAge li input');
      var clicked = listInputs.attr('name');
      //loop on all the li children to see if they match the clicked LI - if not ensure that they are unchecked and faded
      $(listInputs).each(function(index,Element){
        $(listInputs).attr('checked',false).parent().addClass('selected');
      });
      //set the radio button to checked and apply a style to it, while removing this from any other checked radio buttoon
      if($(listInputs).attr('name')==clicked){
        $(element).children().attr('checked','true').parent().removeClass('selected'); 
      } 
    }
      $('#healthCheckAge li').click(function(){
        lol(this);                                
      });
    

    Inside the click handler, this refers to the element, but inside lol it points to the owner of the function, which is the global window object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey--I'm writing a basic Rails app that uses the digg API. I'm trying to
I'm writing a basic text chat web app, and i'm trying to figure out
I'm writing a basic little forums web app (for fun and to sharpen the
I have a basic web app in place: it has a registration page, a
I'm writing a web app that will be making requests via AJAX and would
I'm writing an .NET 3.5 web application that is using LinqToSql for the basic
I'm writing a basic push messaging system. A small change has caused it to
I am writing a basic program in Python that prompts the user to enter
I am writing a Visual Basic IDE, and I need to add lines numbers
I writing real estate web site with basic function for choosing and ordering realty.

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.