Results 1 to 5 of 5

Thread: check checkbox, highlight label

  1. #1
    Join Date
    Feb 2015
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default check checkbox, highlight label

    Hi,

    I have this jsfiddle http://jsfiddle.net/mlotfi/f8t1uysa/ that that two checkboxes, when checking the first one its label get highlithed, but the second checkbox does not have any effect.
    please your help is appreciated.
    Thanks.

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Use the markup and CSS for the working example then

    To highlight the label with CSS only, you need to have the checkbox first, and then you can use the :checked pseudo-class selector in conjunction with adjacent or sibling selectors.

    - use the adjacent sibling selector (+) when the label comes immediately after the checkbox
    - use the general sibling selector (~) when the labels comes somewhere after the checkbox
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    Feb 2015
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I think that what I did :

    for :

    HTML Code:
     <input type="checkbox" id="check" class="check-with-label" />
      <label  class="label-for-check">My Label</label>
    this css :

    HTML Code:
    .check-with-label:checked + .label-for-check {
      background-color: #b0c4de;
    }
    works fine.


    but why it did not work with :

    HTML Code:
    <label class="label-for-check">
            <input type="checkbox" class="check-with-label" value="ssssss"/> SSSSS</label>

  4. #4
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Because it's backwards. You can't check a checkbox and look back UP the DOM with a selector that only goes DOWN the DOM. There isn't currently a selector that goes UP the DOM as an alternative either.

    Like I said in my previous post, to style the label for a checked checkbox with CSS, it needs to come AFTER the checkbox - not inside it or in front of it, because there is no CSS selector to interpret that direction.

    checkbox:checked + label means something like "when the checkbox is checked, target the label immediately after it"

    checkbox:checked ~ label means something like "when the checkbox is checked, target the label somewhere after it"

    What you have that doesn't work is label > checkbox:checked which is like saying "when the checkbox that is inside label is checked... (nothing)". It makes no sense - this way, you are not targeting the label when the checkbox is checked (you are not targeting anything) so no styles can be applied to it.
    Last edited by Beverleyh; 02-23-2015 at 08:13 PM.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  5. #5
    Join Date
    Feb 2015
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Beverleyh View Post
    Because it's backwards. You can't check a checkbox and look back UP the DOM with a selector that only goes DOWN the DOM. There isn't currently a selector that goes UP the DOM as an alternative either.

    Like I said in my previous post, to style the label for a checked checkbox with CSS, it needs to come AFTER the checkbox - not inside it or in front of it, because there is no CSS selector to interpret that direction.

    checkbox:checked + label means something like "when the checkbox is checked, target the label immediately after it"

    checkbox:checked ~ label means something like "when the checkbox is checked, target the label somewhere after it"

    What you have that doesn't work is label > checkbox:checked which is like saying "when the checkbox that is inside label is checked... (nothing)". It makes no sense - this way, you are not targeting the label when the checkbox is checked (you are not targeting anything) so no styles can be applied to it.
    Thanks lot Beverleyh, I learned new things from you, I appreciate it.

Similar Threads

  1. check all checkbox
    By reguapo in forum JavaScript
    Replies: 5
    Last Post: 07-25-2012, 12:23 AM
  2. My ASP label not displaying
    By gib65 in forum ASP
    Replies: 2
    Last Post: 02-03-2011, 03:06 PM
  3. Replies: 0
    Last Post: 05-02-2007, 11:57 AM
  4. Onclick check checkbox then do action
    By motormichael12 in forum PHP
    Replies: 1
    Last Post: 12-02-2006, 05:26 PM
  5. getElementById() and the <label> element
    By harryj in forum JavaScript
    Replies: 0
    Last Post: 02-20-2006, 04:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •