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

  • Home
  • SEARCH
  • 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 8336961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:09:05+00:00 2026-06-09T04:09:05+00:00

I have a function which hides or shows elements with a given class name:

  • 0

I have a function which hides or shows elements with a given class name:

$.fn.questionDetails = function(base_element, details_yes_no) {
  var details_element = '.' + base_element + '_details';
  var radio_element = '#' + base_element + '_' + details_yes_no;

  if ($(radio_element).attr('checked')) {
    $(details_element).show();
  } else {
    $(details_element).hide();
  }
}

I want to attach/bind this function so that it is called with the two parameters when the page is loaded and when a given radio button group is changed. At the moment I’m using the following code to accomplish this:

$(function() {
  // $.fn.questionDetails defined here, in same scope

  $.fn.questionDetails('question1', 'yes');
  $('input:radio[name=question1]').change().questionDetails('question1', 'yes');
}

The first function call works fine – when the page is loaded the question1_details panel will either be shown or hidden. However, the function does not seem to be called correctly when I change which radio button is selected, nor does it work when I modify the change() line to be this:

$('input:radio[name=question1]').change($.fn.questionDetails('question1', 'yes'));

I want to be able to say “call this function, with these parameters, when the page loads and when a given radio button group is changed”, with the least amount of repetition. What is the best way to go about this? I’m relatively new to jQuery, so it’s possible I’m doing things the ‘wrong’ way from the start.

Edit: Should have mentioned this originally – I’m using jQuery 1.7.1.

  • 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-06-09T04:09:07+00:00Added an answer on June 9, 2026 at 4:09 am

    If you want to assign a call to a function that takes parameters (other than the event) to an event handler, you’ll need to wrap that call in an anonymous function, like so:

    $('input:radio[name=question1]').change(function() {
        $.fn.questionDetails('question1', 'yes')
    });
    

    Why? Because the change() function expects a function reference to be passed to it. When you do change($.fn.questionDetails('question1', 'yes')) you’re passing the result of calling $.fn.questionDetails('question1', 'yes') to the function, rather than a function reference.

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

Sidebar

Related Questions

I have a simple function that shows and hides elements if a user clicks
I have a function which looks something like this, it returns a noncopyable class
<div class='hide'>A</div> <div class='hide'>B</div> <div class='hide' id='1'>C</div> I have a function called showOne which
I have a div show hide function which is working fine. when I click
hi i have function which is called by tinker listbox so i cannot return
Can I´m asking for advice. I have function which should return javascript object function
I have a function which I have to loop it a few times but
I have a function which takes two arguments, the ID of an item (wine)
I have a function which takes several boolean template arguments: template<bool par1, bool par2,
I have a function which zips files and downloads them to the users machine.

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.