Use of attr to apply edge color to LI

Asked

Viewed 1,090 times

1

I own a li, which is a square, the CSS is like this

.produtos{
    width: 294px;
    height: 349px;
    /*background-color: #f1f2f2;*/
    border: 3px solid transparent;
    position: relative;
    cursor: pointer;
    transition: all .4s ease-out;
    background-repeat: no-repeat;
}

What happens is I want to change the color of border when the user hovers over. I can’t do that with the hover simple CSS, because this color will be manageable. I already have a reserved word that will apply this color, I just think the ideal would be to use an attr in Jquery, I’m sure?

My HTML is like this:

<li data-cor="codigoSYS" style="background-image: url(fotoSYS&w=294)" class="produtosBorda produtos f-left margin-right-30 margin-top-30"></li>   

code is the hexadecimal value of the color that is received by the manager.

Jquery:

var cor = $('.produtosBorda').attr('data-cor');
$('.produtosBorda').css('border-color', cor);
  • 1

    Do you have an excerpt of html code, css and an example of what the final result would look like? because there may be several solutions there if you can choose the best to your code!

1 answer

4


No, use the method .css() instead of the attr. For example:

$('li').css('border-color', '#00ff00');

If the color comes from a data-attribute in your HTML, no problem. For example:

<li id="sei-la" data-cor="#ff0000">...</li>
var cor = $('#sei-la').attr('data-cor');
$('#sei-la').css('border-color', cor);

To react to the input and output of the mouse in each <li>, something like this should work:

$('li.produtos').mouseenter(function() {
    var li = $(this);
    // guarda cor original num atributo
    li.attr('data-cor-original', li.css('border-color'));
    // define nova cor
    li.css('border-color', li.attr('data-cor'));
}).mouseleave(function() {
    var li = $(this);
    // restaura cor original
    li.css('border-color', li.attr('data-cor-original'));
});
  • Thank you for the reply. I would like to use a attr, because as I commented, this color will be user-manageable. So I can’t set the border color with jquery, but in HTML. I believe.

  • 1

    Are you talking about data-Attributes? I updated my answer, see if that’s it.

  • Exactly that! Note 10. There are only two things I would appreciate if you could help me. I have a listing of elements and it is only applying in the first item. Second, it has how to apply attached in :Hover?

  • Are you doing Hover by jquery, @Felipestoker? The example I gave uses an ID to choose the li that will be affected, but any selector would work. It’s hard to tell what to use in your case without seeing more of your code. You could update the question?

  • Hi, as it is a repeat listing, I took the ID and put it in the class. Funfou, however it applies the same color to everyone. I edited my question.

  • 1

    I edited the answer again, @Felipestoker

  • 1

    Man, it’s PERFECT! Thank you so much!

Show 2 more comments

Browser other questions tagged

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