Tgralak
Tgralak

Reputation: 147

jQuery - hover works only on every 2nd div

I have a problem. I'm creating a divs from input form, but when I hover my mouse with .hover function, it works only on every second div element (first, third, 5th, 7th...). How do I solve that? What's wrong with JS function? Thanks for answers.

JS:

$("#entryButton").click(function(){
  event.preventDefault(); //stops refreshing
  var query = $("#entry").val(); //string z inputa
  if (query !== "") {
    var trashButton = "<button class='trash'>DEL</button>"
    var registry = "<div class='drag'>" + "<p>" + query + "</p>" + trashButton + "</div>"
    $("#list").append(registry); //add div with query and ubbton

    $("#list").sortable({
      //axis: "y",
    });

    $(".drag").hover(function() {
      $(this).toggleClass("mousehover")
    });

    $("#entry").val(""); //clear value

    return false; //also stops refreshing
    console.log(registry);

  }
})

HTML:

<div class="container">
  <form>
    <input type="text" id="entry">
    <button id="entryButton">button</button>
  </form>
  <ul id="list">
  </ul>
</div>

CSS:

body {
  font-size: 14px;
}

form {
  float:right;
}

.container {
  min-width:300px;
  width:20%;
  margin: 0 auto;
  margin-top:5px;
}

.drag {
  margin-top:5px;
  background-color:lemonchiffon;
  display:inline-flex;
  width:100%;
}
.trash {
  position:absolute;
  margin-left:190px;
}

.mousehover {
  opacity:0.5;
}

Upvotes: 1

Views: 72

Answers (2)

imtheman
imtheman

Reputation: 4843

The problem is that you are adding the hover event multiple times. It is better to do it only once, using $(document).on().

$("#entryButton").click(function(){
  event.preventDefault(); //stops refreshing
  var query = $("#entry").val(); //string z inputa
  if (query !== "") {
    var trashButton = "<button class='trash'>DEL</button>"
    var registry = "<div class='drag'>" + "<p>" + query + "</p>" + trashButton + "</div>"
    $("#list").append(registry); //add div with query and ubbton

    $("#list").sortable({
      //axis: "y",
    });

    $("#entry").val(""); //clear value

    return false; //also stops refreshing
    console.log(registry);

  }
});

$(document).on("mouseenter mouseleave", ".drag", function() {
  $(this).toggleClass("mousehover");
});
body {
  font-size: 14px;
}

form {
  float:right;
}

.container {
  min-width:300px;
  width:20%;
  margin: 0 auto;
  margin-top:5px;
}

.drag {
  margin-top:5px;
  background-color:lemonchiffon;
  display:inline-flex;
  width:100%;
}
.trash {
  position:absolute;
  margin-left:190px;
}

.mousehover {
  opacity:0.5;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<div class="container">
  <form>
    <input type="text" id="entry">
    <button id="entryButton">button</button>
  </form>
  <ul id="list">
  </ul>
</div>

Upvotes: 3

Shiladitya
Shiladitya

Reputation: 12181

Here you go with a solution https://jsfiddle.net/wcu4w1mn/

$("#entryButton").click(function(){
  event.preventDefault(); //stops refreshing
  var query = $("#entry").val(); //string z inputa
  if (query !== "") {
    var trashButton = "<button class='trash'>DEL</button>"
    var registry = "<div class='drag'>" + "<p>" + query + "</p>" + trashButton + "</div>"
    $("#list").append(registry); //add div with query and ubbton

    $("#list").sortable({
      //axis: "y",
    });

    $(".drag").last().hover(function() {
      $(this).toggleClass("mousehover")
    });

    $("#entry").val(""); //clear value

    return false; //also stops refreshing
    console.log(registry);

  }
})
body {
  font-size: 14px;
}

form {
  float:right;
}

.container {
  min-width:300px;
  width:20%;
  margin: 0 auto;
  margin-top:5px;
}

.drag {
  margin-top:5px;
  background-color:lemonchiffon;
  display:inline-flex;
  width:100%;
}
.trash {
  position:absolute;
  margin-left:190px;
}

.mousehover {
  opacity:0.5;
}
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="container">
  <form>
    <input type="text" id="entry">
    <button id="entryButton">button</button>
  </form>
  <ul id="list">
  </ul>
</div>

Only changed code Add hover event to only last added element.

$(".drag").last().hover(function() {
  $(this).toggleClass("mousehover")
});

Hope this will help you.

Upvotes: 2

Related Questions