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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:44:02+00:00 2026-05-12T10:44:02+00:00

Ok, I have a label, inside this label is a div, which contains an

  • 0

Ok, I have a label, inside this label is a div, which contains an image, and some text. The div has an onClick call to a javascript function, that changes the color of the div inside the label, and also checks the checkbox (for some reason, IE and firefox didn’t want to check it correctly, chrome worked fine).
Javascript:

<script language="javascript" type="text/javascript">
   function changecolor(id, chck1, firstcolor, secondcolor)
                {

                    divid = document.getElementById(id);
  chkboxid = document.getElementById(chck1);
  if(chkboxid.checked){
                    divid.style.background= firstcolor;
   var ua = navigator.userAgent.toLowerCase();
   if (ua.indexOf('safari/') == -1){
    chkboxid.checked = false ;
   }
  }
  else{
  divid.style.background= secondcolor;
   var ua = navigator.userAgent.toLowerCase();
   if (ua.indexOf('safari/') == -1){
    chkboxid.checked = true ;
   }
  }
                }

html:

<input type="checkbox" id="idbox" name="cboxwithlabel" value="Another check box"><label for="idbox"><div class="listing" onClick="changecolor('Test33', 'idbox', '#09C', '#0F0');" id="Test33">Another checkbox<img src="images/checkmark.png" height="80" width="80" /></div></label>

Ok, so using this works fine in Firefox, and Chrome, but IE 7 (haven’t tested other versions yet), it will only check the checkbox if you click the image inside the div. Clicking the div itself, only changes the background color. So what is the fix to have IE work so that when you click the div it also checks the checkbox?

  • 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-12T10:44:03+00:00Added an answer on May 12, 2026 at 10:44 am

    One of the nice things that browsers do, is triggering click events from <label> elements for us.

    So my recommendation is to attach the click handler to the checkbox itself. This way your function becomes MUCH simpler.

    Also, it’s illegal to put block-level elements inside of line-level elements, so I’ve changed your <div> to a <span> and used CSS to make it render as a block-level element.

    <html>
    <head>
    
    <script language="javascript" type="text/javascript">
      function changecolor( cbox, divId, firstcolor, secondcolor )
      {
        document.getElementById( divId ).style.background = cbox.checked ? firstcolor : secondcolor;
      }
    </script>
    
    <style type="text/css">
      span.listing {
        display: block;
      }
    </style>
    
    </head>
    <body>
    
    <input type="checkbox" id="idbox" name="cboxwithlabel" value="Another check box" onClick="changecolor(this, 'Test33', '#09C', '#0F0');">
    <label for="idbox">
      <span class="listing" id="Test33">
        Another checkbox<img src="images/checkmark.png" height="80" width="80"  onclick="this.parentNode.click()" />
      </span>
    </label>
    
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list like this: <ul> <li id=adm-thumb onclick=javascript:addBanner('bowling.jpg');> <div class=adm-tick></div> <img src=img/banners/bowling.jpg
I have a div element that has an id and this div contains a
I have a problem with this function: $(#contentdiv).click(function() { $(this).append(<div class='editable'>For some reason this
I need to have a for loop inside my jQuery. Example: for(i=0;i<counter;i++) { $("div"+i+"").click(function(){//some
I have this label control in my web page <asp:Label ID=Label1 runat=server Text=test></asp:Label> And
I want to create a <div> which has a Label/Header displayed in Capitals letters.
I have a div called subMenuRigth this div it's inside a <li> what i
I have a label function like : private function formatDate (item:Object, column:DataGridColumn):String { var
I have a label on my asp.net page, it looks like this: more info
I have a gender selection radio: <div class="label-inputs" name="userFieldDiv" id="genderUserFieldDiv"> <label class="required">Sexo</label> <input type="radio"

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.