Javascript - Function that changes the background of the parent element

Asked

Viewed 40 times

1

I’m having trouble making a Function. Can help me?
After clicking on the checkbox or on the label, it colors only the word, I would like to color also the background of the div, I tried to make a script like:
var obj = document.getElementById("teste").parent(); obj.style.backgroundColor='#FF0000';
But it didn’t work, you can help me??

    function teste(){
       var obj = document.getElementById("categoria").parent();
         obj.style.backgroundColor='#FF0000'; 
    }
input.check-genero:hover + label,
input.check-genero:checked + label{
    -webkit-transform: scale(1.2);
    -ms-transform: scale(1.2);
    transform: scale(1.2);
}

/* Tabela conteúdo */
table.tabela-categoria{
    border: solid 15px #fff;
}

.tabela-categoria td{
    border: solid 2px #d3d1d1;
    text-align: center;
    padding: 5px;
    width: 140px;
}

.categoria + label{
    font-family: verdana;
    font-size: 1.2em;
    color: #727176;
    cursor: pointer;
}

.categoria:hover + label,
.categoria:checked + label{
    color: #ffffff;
    background-color:#FF6600;
}
<table class="tabela-categoria">
    <tr>
        <td class="tabela-categoria">
            <input type="checkbox" name="cont1" id="animais" class="categoria">
            <label for="animais" onclick="teste()">ANIMAIS</label>
        </td>
        <td>
            <input type="checkbox" name="cont2" id="artes" class="categoria">
            <label for="artes">ARTES</label>
        </td>
        <td>
            <input type="checkbox" name="cont3" id="beleza" class="categoria">
            <label for="beleza">BELEZA</label>
        </td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" name="cont4" id="business" class="categoria">
            <label for="business">BUSINESS</label>
        </td>
        <td>
            <input type="checkbox" name="cont5" id="causas" class="categoria">
            <label for="causas">CAUSAS</label>
        </td>
        <td>
            <input type="checkbox" name="cont6" id="comedia" class="categoria">
            <label for="comedia">COMEDIA</label>
        </td>
    </tr>
</table>

  • 1

    Vc is using Document.getElementById("category") but there is no element in your HTML with the id category

2 answers

1


Pass the event to that function and then do e.target.closest('td') to find the td.

Example:

function teste(e) {
  var el = e.target.closest('td');
  el.style.backgroundColor = '#FF0000';
}
input.check-genero:hover+label,
input.check-genero:checked+label {
  -webkit-transform: scale(1.2);
  -ms-transform: scale(1.2);
  transform: scale(1.2);
}


/* Tabela conteúdo */

table.tabela-categoria {
  border: solid 15px #fff;
}

.tabela-categoria td {
  border: solid 2px #d3d1d1;
  text-align: center;
  padding: 5px;
  width: 140px;
}

.categoria+label {
  font-family: verdana;
  font-size: 1.2em;
  color: #727176;
  cursor: pointer;
}

.categoria:hover+label,
.categoria:checked+label {
  color: #ffffff;
  background-color: #FF6600;
}
<table class="tabela-categoria" onclick="teste(event)">
  <tr>
    <td class="tabela-categoria">
      <input type="checkbox" name="cont1" id="animais" class="categoria">
      <label for="animais" >ANIMAIS</label>
    </td>
    <td>
      <input type="checkbox" name="cont2" id="artes" class="categoria">
      <label for="artes">ARTES</label>
    </td>
    <td>
      <input type="checkbox" name="cont3" id="beleza" class="categoria">
      <label for="beleza">BELEZA</label>
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" name="cont4" id="business" class="categoria">
      <label for="business">BUSINESS</label>
    </td>
    <td>
      <input type="checkbox" name="cont5" id="causas" class="categoria">
      <label for="causas">CAUSAS</label>
    </td>
    <td>
      <input type="checkbox" name="cont6" id="comedia" class="categoria">
      <label for="comedia">COMEDIA</label>
    </td>
  </tr>
</table>

  • Got it @Sergio !! , I will study and apply the teaching, thank you !!

  • could do this with css? for example : .categoria:has(td a:hover){

  • 1

    @Sora CSS has no selectors for ancestors. According to the name cascading it only applies to descendants and sibling elements.

  • 1

    I understand, thank you for the explanation !!

1

With Document.getElementById("category") you are looking for an element with id category but it does not exist in your html, you need to search for an element with the category class. For this you can use querySelector because it searches for classes in html, as well as ids. See in the code below, tested and worked.

function teste(){
  var obj = document.querySelector(".categoria").parentNode;
  obj.style.backgroundColor='#FF0000'; 
}

The . Parent() method also does not work because it is jQuery, with pure JS use parentNode.

  • Aaah, I get it !! Thank you very much :D

  • It would have to do with css through the .categoria:has(td a:hover){ ? This has seems that it can also serve and much better, because it solves everything by css

  • 1

    The :has is a pseudo-class css but it’s still in test, don’t use it yet. See on this site, in the browser compatibility table: https://developer.mozilla.org/en-US/docs/Web/CSS/:has Also, no compatibility with browsers: https://caniuse.com/#search=%3Ahas

  • I understand, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.