How to leave option checked when div

0

There is a site where the client should choose cor and tamanho for a given product, what I have today are divs that show the colors and sizes but I am not able to leave the marcada option when it is selected , such as radios. I need the values for a check when they are not marked.

The css are these:

.tovar_color_select {padding-bottom:19px;}
.tovar_color_select p {
    margin-bottom:13px;
    text-transform:uppercase;
    font-weight:900;
    font-size:12px;
    color:#333;
}
.tovar_color_select a {
    position:relative;
    display:inline-block;
    margin-right:10px;
    width:32px;
    height:22px;
}

.tovar_color_select a:before {
    content:'';
    position:absolute;
    left:-4px;
    top:-4px;
    right:-4px;
    bottom:-4px;
    border:1px solid #e9e9e9;
    transition: all 0.1s ease-in-out; 
    -webkit-transition: all 0.1s ease-in-out;
}
.tovar_color_select a:hover:before,
.tovar_color_select a.active:before {
    border:2px solid #333;
}

.tovar_size_select {padding-bottom:25px;}
.tovar_size_select p {
    margin-bottom:9px;
    text-transform:uppercase;
    font-weight:900;
    font-size:12px;
    color:#333;
}
.tovar_size_select span {
    float:right;
    display:block;
    line-height:18px;
    font-size:12px;
    color:#666;
}
.tovar_size_select a {
    display:inline-block;
    width:40px;
    height:30px;
    margin:0 1px 5px;
    text-align:center;
    line-height:28px;
    font-size:11px;
    color:#666;
    border:1px solid #e9e9e9;
    transition: color 0.3s ease-in-out, border-color 0.2s ease-in-out;
    -webkit-transition: color 0.3s ease-in-out, border-color 0.2s ease-in-out;
}
.tovar_size_select a:hover,
.tovar_size_select a.active {
    line-height:26px;
    color:#333;
    border:2px solid #333;
}

What I have so far is this:

<div class="tovar_color_select">
  <p>SELECIONE A COR</p>
  <?php foreach($ResCor as $ProdCor) { ?>
    <a IdCor="<?php echo $ProdCor->IdCor; ?>" style="background-color:<?php echo $ProdCor->Cor ?>;"  ></a>
  <?php } ?>

</div>

<div class="tovar_size_select">
  <div class="clearfix">
    <p class="pull-left">SELECIONE O TAMANHO</p>									
  </div>
  <?php foreach($ResTamanho  as $ProdTam) { ?>
    <a IdTamanho="<?php echo $ProdTam->IdTamanho; ?>" ><?php echo $ProdTam->Nome; ?></a>
  <?php } ?>
</div>

What I'm trying to do is what's in this link:

My site is this: Site

    
asked by anonymous 16.08.2018 / 16:20

2 answers

1

You can get the id for example from the div that was clicked to use its value.

$(document).ready(function(){
    $(".divs").on("click", function(){          // pega click na div
      $(".divs").each(function(){               // para cada div
        $(this).removeClass("selecionado");     // remove a classe selecionada
      })
      $(this).addClass("selecionado");          // adicona a classe no click
      console.log($(this).prop("id"));          // mostra a div selecionada
    })      
})
#div-pai{
  width: 150px;
  display: flex;
  justify-content: space-between;
}
.divs{
  width: 25px;
  height: 25px;
  cursor: pointer;
}
#amarela{
  background-color: yellow;
}
#vermelha{
  background-color: red;
}
#azul{
  background-color: blue;
}
.selecionado{
  border: solid 3px #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><divid="div-pai">
  <div class="divs" id="amarela"></div>
  <div class="divs" id="vermelha"></div>
  <div class="divs" id="azul"></div>
</div>
    
16.08.2018 / 17:00
1

First create a class in every <a> :

<a class='tamanho' IdTamanho="<?php echo $ProdTam->IdTamanho; ?>" ><?php echo $ProdTam->Nome; ?></a>

Then, create a js code:

$('.tamanho').on('click', function () { 
   $('.tamanho').each(function() {
      $(this).removeClass('selecionado');
   });
   $(this).addClass('selecionado');
});

Then you create a 'selected' class in the css and apply the border or style you want

    
16.08.2018 / 16:40