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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:20:20+00:00 2026-05-24T20:20:20+00:00

I have a unique little problem. I have to show and hide a container,

  • 0

I have a unique little problem. I have to show and hide a container, when a specific radio button is selected. Unfortunately my event listeners are being fired when declared, and not when the event actually occurs. Has anyone else run in to this sort of issue?

<!DOCTYPE html>
<html>
    <head><title>test</title></head>

<body>
    <form name="form">
       <input type="radio" name="r1" id="r1_yes" value="yes">
       <input type="radio" name="r1" id="r1_no" value="no">
       <div id="r1_comments" class="comments">Comments r1</div>
       <br />

       <input type="radio" name="r2" id="r2_yes" value="yes">
       <input type="radio" name="r2" id="r2_no" value="no">
       <div id="r2_comments" class="comments">Comments r2</div>
    </form>

<script type="text/javascript">
function checkCommentTrigger(element, comments, trigger) {
    if (element.value===trigger && element.checked) { comments.style.display="block"; }
    else { comments.style.display="none"; }
}

function comments(fieldName, commentsID, triggerValue) {
    var t_elements, t_comments, i;

    t_elements = document.getElementsByName("fieldName");
    t_comments = document.getElementById(commentsID);

    t_comments.style.display="none";        


    for (i=0; i<t_elements.length; i++) {
        if (t_elements[i].addEventListener){t_elements[i].addEventListener("click",checkCommentTrigger(this, t_comments, triggerValue),true);}
        else {t_elements[i].addEvent('onclick',checkCommentTrigger(this,t_comments,triggerValue));}
    }
}

comments("r1","r1_comments","no");
comments("r2","r2_comments","no");

</script>
</body>
</html>
  • 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-24T20:20:21+00:00Added an answer on May 24, 2026 at 8:20 pm

    You’re calling the function in your “addEventListener” call. That is, in this code (broken into two lines):

       if (t_elements[i].addEventListener){
          t_elements[i].addEventListener("click",checkCommentTrigger(this, t_comments, triggerValue),true);}
    

    you’re calling the “checkCommentTrigger()” function. What you have to do is pass in a reference to a function, so something like this:

       if (t_elements[i].addEventListener){
          t_elements[i].addEventListener("click",function() { checkCommentTrigger(this, t_comments, triggerValue) },true);}
    

    Same for the other branch of the “if”:

       else {
          t_elements[i].attachEvent('onclick',function() { checkCommentTrigger(this,t_comments,triggerValue) });}
    

    Also, you want “attachEvent()” for IE, not “addEvent()”.

    edit — looking at this again, there’ll be a problem in that code if you use it as I wrote above. The problem will stem from the reference to this in the function to use as an event handler. I think that you want the first argument to “checkCommentTrigger” to be the element to be checked. It might be simplest to set up a separate function:

      function handlerFor(element, comments, value) {
        return function() {
          checkCommentTrigger(element, comments, value);
        }
      }
    

    Then you can call that when you need an event handler function:

      if (t_elements[i].addEventListener){
          t_elements[i].addEventListener("click", handlerFor(t_elements[i], t_comments, triggerValue), true);
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a unique situation over here. I have a button on a form
I have a little problem with encoding. The data in db is ok, when
I'm very confused with this wee little problem I have. I have a non-indexed
I have made a little app for signing up for an event. User input
I have an SQL database with a set of tables that have Unique Id's.
I'm wondering if it is possible to have unique characters that cannot be seen
I have a unique industrial application where I do not want the Windows CE
I have a unique requirement that I hope is possible with LINQ to SQL.
I have a unique situation where I'm building a site that will call data
I have a unique requirement which I need to meet to with sitecore, and

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.