Problem with CSS in Jquery

Asked

Viewed 44 times

2

I have the following HTML:

$(".membrosClick").click(function() {
  $(".membrosClick").css("width", "766px");
  $(this).children().fadeToggle();
  $(".listagemEsc").css("margin-left", "383px");
});
<li class="membrosClick">
  <div name="Listagem de Membros" style="background-image: url('./imagens/EDGAR-GUERRA.png'); width:383px; height:849px">
    <div class="editable subir">
      <h2>Edgar</h2>
      <h3>Guerra</h3>	
      <div class="bandaBarra bandaBarraBranca margin-left-65 margin-top-10 barraNone"></div>
    </div>

  </div>
  <div class="listagemEsc p-relative">
    <div class="membrosFechar"></div>
    <span>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo.</span>
  </div>
</li>
<li class="membrosClick">
  <div name="Listagem de Membros" style="background-image: url('./imagens/ANTONIO-GUERRA.png'); width:383px; height:849px">
    <div class="editable subir">
      <h2>Antônio</h2>
      <h3>Guerra</h3>	
      <div class="bandaBarra bandaBarraBranca margin-left-65 margin-top-10 barraNone"></div>
    </div>

  </div>
  <div class="listagemEsc p-relative">
    <div class="membrosFechar"></div>
    <span>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo.</span>
  </div>
</li>

When I click on li he has to expand her to width: 766px and send the div listagemEsc for margin-left:383px. He’s expanding to 766px only he does it at all li, and not just the one I clicked on. I couldn’t test the rest.

  • Ever tried to use $(this).css("width", "766px");?

1 answer

2


Two amendments:

  • uses $(this).css() to apply only to the element you clicked
  • uses .next() to fetch the brother div immediately afterwards, instead of calling on the selector all elements with the class listagemEsc. Or to be more specific el.closest('.membrosClick').find('.listagemEsc').css("margin-left", "383px");

Final result:

$(".membrosClick").click(function () {
    var el = $(this);
    el.css("width", "766px");
    el.children().fadeToggle();
    el.next().css("margin-left", "383px");
});
  • 1

    It worked perfectly, I had confused things before! thank you very much

  • can I raise the question? I have another question now, but it relates to something that happens after that.

  • 1

    @Felipestoker is another question. That’s the policy of the site. More useful with specific problems. I don’t know if this is it but I was going to suggest you css Transitions for these margin and width changes to be like animation.

  • That’s not it, I’ll ask another question then, thank you :D

Browser other questions tagged

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