0

I want to edit the rendering of an element when hovering. To achieve this, I created a button and I want the first click to set the hover rendering and the second click to reset the hover rendering. Currently, the hover style appears even when I'm not on the div:

$("#editer-script").click(function() {
  var clicks = $(this).data('clicks');

  if (clicks) {
    $('.contenu-editable').mouseover(function(){
        $('.contenu-editable').css("background-color", "transparent");
        $('.contenu-editable .fa.fa-pencil').css("display", "none");
    });
    $("#editer-script").text('Rendre éditable');
  } else {
    $('.contenu-editable').mouseover(function(){
        $('.contenu-editable').css("background-color", "#f4f6f9");
        $('.contenu-editable .fa.fa-pencil').css("display", "inline-block");
    });
    $("#editer-script").text('Ne pas rendre éditable');
  }

  $(this).data("clicks", !clicks);
});

Thank you in advance for your help.

4
  • question is not clear Commented Mar 23, 2018 at 10:51
  • use mouseenter Commented Mar 23, 2018 at 10:51
  • @KiranShahi, I edited my question for clarity. Commented Mar 23, 2018 at 10:54
  • Please provide html too Commented Mar 23, 2018 at 10:55

1 Answer 1

1

I would use two different css classes. One default and one for the change of the hover effects. And then just toggle the second class on click of the button element.

It's mutch easier than try to do it your way and it could be easily changed and extended just in css. No need to touch the script in the future.

$("#editer-script").click(function() {
  var clicks = $(this).data('clicks');

  $('.contenu-editable').toggleClass('special', !clicks);
  $("#editer-script").text(clicks ? 'Rendre éditable' : 'Ne pas rendre éditable');

  $(this).data("clicks", !clicks);
});
.contenu-editable:hover {
  background-color: transparent;
}

.contenu-editable:hover .fa.fa-pencil {
  display: none;
}

.contenu-editable.special:hover {
  background-color: #f4f6f9;
}

.contenu-editable.special:hover .fa.fa-pencil {
  display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="editer-script">Rendre éditable</button>
<div class="contenu-editable">
  <i class="fa fa-pencil">pencil</i>
  content
</div>

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but it seems that classes are not added when I click on the #edit-script div. The text changes correctly...
How can I add the "sortable" feature in the same way as the "special" class ?
That is a bit mutch for this question and would be off-topic here. Please create a new question with the changes you did, and tell us whats the problem and what you want to do. We will help you then within the new question. ;) @Pierre-ClémentCazon

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.