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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:45:14+00:00 2026-05-29T14:45:14+00:00

My HTML looks something like this (unfortunately it cannot change because it is being

  • 0

My HTML looks something like this (unfortunately it cannot change because it is being rendered with the CMS I am using):

<li>
<input id="cb1" type="checkbox">
<label for="cb1">
<div>Some Other Content</div>
<div class="pledge">Click to Pledge</div>
</label>
</li>

The jQuery.

$("div.pledge").click(function() {

  var checkboxlabel = $(this).parent("label");

  $("input[for=checkboxlabel]").attr('checked', !$checkbox.attr('checked'));

});

Using this code will toggle the checkbox if I click anywhere inside the label. What I need is for the action to only take place when I click the <div class="pledge"> element. I suspect that .stopPropagation(); will help me here but I have not been able to get it to work properly.

Here is another shot that seems to be accomplishing the same think:

$("div.pledge").click(function() {

  $("input[for=' + this.parent("label").attr("id") + ']").attr('checked', !$checkbox.attr('checked'));

});
  • 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-29T14:45:17+00:00Added an answer on May 29, 2026 at 2:45 pm

    Reading between the lines, it seems to me that the following will achieve your desired result without any JS:

    <li>
    <input id="cb1" type="checkbox">
    <div>
    <div>Some Other Content</div>
    <label for="cb1">Click to Pledge</label>
    </div>
    </li>
    

    http://jsfiddle.net/nnnnnn/VqmBb/

    This displays “Some Other Content” and “Click to Pledge” next to the checkbox, but as you can see with my demo only the “Click to Pledge” part checks/unchecks the box when clicked.

    I’ve swapped your label and the “pledge” div around, and made use of the label’s for attribute to associate it with the checkbox so that it will automatically check/uncheck it. (Though now the outer div doesn’t actually do anything and could be removed unless you need it for styling.)

    Regarding your JS, the following line has some problems:

    $("input[for=checkboxlabel]").attr('checked', !$checkbox.attr('checked'));
    

    First, your selector "input[for=checkboxlabel]" seems to be trying to use the checkboxlabel variable declared on the line before, but what you’ve actually done is just include a string that happens to include the text “checkboxlabel”. So that won’t select any elements in your html because you have no “input” element with a “for” attribute equal to the string “checkboxlabel”. Also you’ve got a variable $checkbox that isn’t defined anywhere.

    UPDATE: OK, the following should work for your updated html (it worked for me). The problem with your existing JS code, apart from the fact that you are trying to use a variable, $checkbox, that isn’t declared, is that you are trying to bind your click handler to the “pledge” div which means you haven’t done anything to handle clicks on other parts of the label. Given that you specifically want to stop clicks on other parts of the label I’d suggest that you attach the .click() handler to the label – clicks on the label’s children will bubble up to the label’s click handler, so then you can test whether the element that was clicked was the one with class “pledge”. If not return false to cancel the default behaviour for a label click, but if it is the “pledge” element do nothing and let the default happen:

    $("div.pledge").parent("label").click(function(e) {
        if (!$(e.target).hasClass("pledge"))
            return false;
    });
    

    (Note I’m selecting the parent of “div.pledge” rather than just saying $("label").click() so that this processing doesn’t get attached to all labels.)

    If you don’t like that I think mgibsonbr’s answer is a nice alternative.

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

Sidebar

Related Questions

My HTML looks something like this: <table class=disabled> <tr> <td> <input blah blah> </td>
The HTML looks something like this: <li class=divider> <div> ...(maybe more divs)... <div class=content>
I have HTML which looks something like this: <span id=text> <span class=segment id=first>some text</span>
I have code that looks something like this: <html> <head> <script type=text/javascript src=http://code.jquery.com/jquery-latest.min.js></script> <script
My html looks something like this: <p>blah blah blah blah blah blah <b>something that
Lets say, the code looks something like this: if(!$test) { header(Location: somefile.html); ...some PHP
My XML file looks something like this: <doc> <RU1> <conf> <prop name=a val=http://a.org/a.html> </conf>
I've got html that looks something like this: <div id=sortThis> <div id=1>Price:<span class=price>20</span><span class=style>blue</span></div>
I have a html table that looks something like this <table id=eventTable> <thead> <tr><th>#</th><th>Options</th></tr>
How do I alternate HTML table row colors using JSP? My CSS looks something

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.