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.

Highlight color selection

Discussion in 'jQuery' started by Mattis23, Sep 8, 2017.

  1. #1
    I am looking for a solution with this jquery script which is set to highlight color on the table using the form below. Default color is green, and I can pick the color I want by clicking the color button. This code works only on inputs with class="selector". I wanted this function to work for inputs with class="all" too, so I duplicated the code and changed $('.selector') to => $('.all') in the second copy. but I couldn't get the result I wanted, the code doesn't work for class="all" selection. Instead it highlights all data on the table. Only the form elements should be highlighted. Fiddle: https://jsfiddle.net/5z7zbaq2/ I would appreciate some help to fix this problem. Thanks.
    SEMrush
    $(function () {
      $('.selector').on('click', function(e) {
        var checked = this.name;
        var selectedColor = '';
    
        if (this.checked) {
          selectedColor = $('#nextColor').val();
    
          }
        $('td').filter(function() {
          return this.textContent == checked;
        }).css('background-color', selectedColor);
      });
     
      $('.all').on('click', function(e) {
        var checked = this.name;
        var selectedColor = '';
    
        if (this.checked) {
          selectedColor = $('#nextColor').val();
    
          }
        $('td').css('background-color', selectedColor);
      });
    });
    Code (JavaScript):
     
    Mattis23, Sep 8, 2017 IP
    SEMrush
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,619
    Likes Received:
    713
    Best Answers:
    150
    Trophy Points:
    420
    #2
    Because you're not filtering the background-color elements in your .all-selector. Look at how it's done in the previous function. Also, if you don't want to filter on table cells, but form elements, you might wanna match on the form elements and color the parent (the td) of those.
     
    PoPSiCLe, Sep 20, 2017 at 3:20 PM IP
  3. Mattis23

    Mattis23 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Thanks, but I don't know how to do necessary changes.
     
    Mattis23, Sep 20, 2017 at 4:49 PM IP