-1
I have a page that there are four Divs, all with class . coloritem:
<div class="coloritemSelected" style="background: Vermelho;" onclick="javascript:showOptionsFor('Vermelho');"> </div>
<div class="coloritem" style="background: red;" onclick="javascript:showOptionsFor('Azul');"> </div>
<div class="coloritem" style="background: red;" onclick="javascript:showOptionsFor('Púrpura');"> </div>
<div class="coloritem" style="background: red;" onclick="javascript:showOptionsFor('Laranja');"> </div>
I wonder if there’s any way to take only the div that has such a color, like Red and change it to have the background: red;
I was testing with that code, but he ends up putting the color on all the Divs:
$(document).ready(function () {
var item = $('.coloritem');
var background = item.attr('style');
if (background == 'background:Vermelho;') {
$(item).css('background', 'red');
};
});
From now on I appreciate any help.
Is there a click event for example that triggers this change?
– Darlei Fernando Zillmer
I was thinking of performing this event when the page loads, since when loading the Divs appear blank.
– Luccas
Puts colors in a type attribute
data
instead ofstyle
, for example:data-cor="vermelho"
, from there on jQuery you read this property through the$('.coloritem').data('cor')
...– user98628
Pera you will run a JS to change the colors when the page loads? Why not create a class for each color and already leave in the right Ivs?
– Darlei Fernando Zillmer
@Pauloimon the point is that the page I’m working on is a store template, and I’ve looked for the location of the element in the directory, but I couldn’t find it (the pages are all separate, with the extension. tpl)
– Luccas
I don’t understand what you want to happen, when one clicks on showOptionsFor, what should happen?
– Zorkind
Thanks for everyone who tried to help, I was able to find the source of the archive, so I was able to do what I planned with the tip @Pauloimon gave.
– Luccas