0
$(document).ready(function() {
$('div#bloqueioSelect').click(function() {
if ($('div#bloqueioSelect ul').is(':visible')) {
$('div#bloqueioSelect ul').css('display', 'none');
$('div#bloqueioSelect div img').prop('src', 'imgs/setaBaixo.jpg');
} else {
$('div#bloqueioSelect ul').css('display', 'block');
$('div#bloqueioSelect div img').prop('src', 'imgs/setaCima.jpg');
}
});
$('div#bloqueioSelect ul li').click(function() {
$('input[type=hidden]#bloqueio').val($(this).val());
});
});
* {
margin: 0;
padding: 0;
border: 0;
}
div.selecao {
position: relative;
border: 1px #CCCCCC solid;
}
div.selecao div {
height: 30px;
text-align: center;
cursor: pointer;
}
div.selecao div,
div.selecao ul {
display: block;
width: 100%;
background-color: #F8F8F8;
}
div.selecao div * {
display: inline-block;
vertical-align: top;
}
div.selecao div label {
width: calc(100% - 30px);
height: 30px;
line-height: 30px;
}
div.selecao ul {
position: absolute;
left: -1px;
/*Menos 1 pixel da borda*/
top: 30px;
display: none;
border: 1px #CCCCCC solid;
}
div.selecao ul li:not(:first-child) {
border-top: 1px #CCCCCC solid;
}
div.selecao ul li {
display: block;
width: 100%;
height: 30px;
cursor: pointer;
text-align: center;
}
div.selecao ul li:hover {
background-color: #CCCCCC;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type=hidden value='' name=bloqueio id=bloqueio />
<div class=selecao style='width: 150px;' id=bloqueioSelect>
<div><label>Bloqueio</label><img src="imgs/setaBaixo.jpg" /></div>
<ul>
<li value=1><img src="imgs/bloquear.png" title="Produto Bloqueado" /></li>
<!--
-->
<li value=0><img src="imgs/desbloquear.png" title="Produto Desbloqueado" /></li>
</ul>
</div>
I have this code who stylized a select.
In that code there is a Javascript for popular one input Hidden
I wonder if there is a way to generalize this function in such a way that I do not need to make a call if I create several id’s for the selects.
Example: this code will only work for the id=lockSelect.
If I have another select, for example: id=citySelect, won’t work.
Or is there some resource without the need to make the call in the field itself?
<div class=selecao style='width: 150px;' id=bloqueioSelect onclick='funcao();'>
That’s what I was trying to avoid
Good. Come on: 1) You used customSelect but could you have used selector? If yes, porrque did not use° 2) Otherwise, because using customSelect did not affect the arrows of the 2 selcts?
– Carlos Rocha
<div>
I look for the elements related to it and not on the whole screen: it means "inside this element that I clicked($(this)
) find such an element (find(...)
)– edson alves
Yes. But technically, div.customSelect, both div’s are. That is, it has the same class. Why didn’t the clike affect both? That is, because when clicking on a div, the input of the other also did not receive the value? That and the doubt!
– Carlos Rocha
You mean, why does clicking one not affect another? Or is clicking not working on any of them for you?
– edson alves
Next: You put . customSelect in the 2 Ivs. Right? If both are. customSelect, I think that, when vlicar in one, the other should also trigger Jquery and consequently its input would also receive the value. But that is not the case. Ants, and correctly, only the input of the applied partition receives the value. I didn’t understand why the other input didn’t also receive the value being that both are .customSelect. That’s why I used id instead of class
– Carlos Rocha
The reason is the same as before... By clicking on
$('.customSelect ul li')
the event is triggered and from it I searched the input:$(this).parents('.customSelect').find("input").val($(this).val());
That means, "Look for the.customSelect
father of thatli
I clicked and then found an input inside it and added the value." The "secret" is in the$(this).parents(...)
– edson alves
I get it. But check me if I’m wrong. When we click on an element, $( 'div#lockSelect' ).click( Function() {} everything inside it that is restrained by $(this), even if there is another div with the same class, it will not receive the action because this only refers to the element clicked while if I look for a ul inside that div thus $( 'div#lockSelect ul' ).css( 'display', 'None' );, this will look in the whole document and not only inside the locked div class WHICH WAS clicked. I got it right?
– Carlos Rocha
@Carlosrocha Just like that.
– edson alves
Thanks friend@
– Carlos Rocha