1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

When button checked change background color of containing div.

Discussion in 'JavaScript' started by makamo66, Jun 3, 2016.

  1. #1
    <script>
    $('.radio-btn').click(function() {
    if( $(this).is(':checked')) {
    $document.getElementsByClassName("row").addClass("hvr");
    }
    });
    </script>

    .hvr {
    background-color: #f2f2f2;
    }

    <div class="row"><input type="radio" class="radio-btn" name="payment-options" /></div>

    When I click on the radio button I want the background color of the containing div to change. What I have here isn't working. Can anybody help?
     
    makamo66, Jun 3, 2016 IP
  2. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #2
    
    $('.radio-btn').click(function() {
      if ($(this).is(':checked')) {
      $('.row').addClass("hvr");
      }
    });
    
    Code (markup):
     
    Blizzardofozz, Jun 3, 2016 IP
  3. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    Thank you. That worked but there's still a problem. I have several divs with the class of row and I only want the one containing the checked radio button to have a different background color.
     
    makamo66, Jun 3, 2016 IP
  4. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #4
    Change the class of the div you want to change or if it is just one div you can add an id.

    
    <div class="row" id="radioBtn"></div>
    
    $('.radio-btn').click(function() {
    if ($(this).is(':checked')) {
    $('#radioBtn').addClass("hvr");
    }
    });
    
    Code (markup):
     
    Blizzardofozz, Jun 3, 2016 IP
  5. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    This works but there's still a problem:

    $('.radio-btn').click(function() {
    if( $(this).is(':checked')) {
    $(this).filter(':checked').closest('.row').addClass("hvr");
    }
    });

    If the user changes his mind and clicks on a different radio button then it has to be highlighted as well. The way it works now it leaves the previously checked button's div highlighted and then highlights the next one as well.
     
    makamo66, Jun 3, 2016 IP
  6. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #6
    The script should check the status of all buttons and remove the hvr class from those not checked.
     
    Blizzardofozz, Jun 3, 2016 IP
  7. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #7
    This is the right code:

    $('.radio-btn').click(function() {
    $(this).filter(':checked').closest('.row').addClass("hvr");

    });

    But there's still the problem that the previously highlighted div remains highlighted when the next radio button is clicked.
     
    makamo66, Jun 3, 2016 IP
  8. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #8
    Because you have to remove the class hvr from the unchecked buttons. You have only addClass method.
     
    Blizzardofozz, Jun 3, 2016 IP
  9. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #9
    How do I do that? My javascript skills are very weak.
     
    makamo66, Jun 3, 2016 IP
  10. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #10
    This works now. Problems solved!

    <script>
    $('.radio-btn').click(function() {
    $('.row').removeClass("hvr");
    $(this).filter(':checked').closest('.row').addClass("hvr");
    });
    </script>
     
    makamo66, Jun 3, 2016 IP
  11. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #11
    Sorry but I was not here. Yes, this is fine.
     
    Blizzardofozz, Jun 3, 2016 IP