How to: Add/Remove Class on mouseOver/mouseOut - JQuery .hover?
Asked Answered
S

4

31

Looking to change the border color on a box..

..when the user mouses over/out..

Here's the attempted code.. Needs Work!

JQuery:

<script>
$("result").hover(
  function () {
    $(this).addClass("result_hover");
  },
  function () {
    $(this).removeClass("result_hover");
  }
);
</script>

CSS3:

<style>
  .result {
    height: 72px;
    width: 100%;
    border: 1px solid #000;
  }
  .result_hover {
    border: 1px solid #fff;
  }
</style>

HTML5:

<div class="result">
  <div class="item">
    <div id="item1">
      <i class="icon"></i>&nbsp;##
    </div>
  </div>
<div>

Thanks for looking

Soleure answered 29/5, 2013 at 18:44 Comment(0)
C
72

You forgot the dot of class selector of result class.

Live Demo

$(".result").hover(
  function () {
    $(this).addClass("result_hover");
  },
  function () {
    $(this).removeClass("result_hover");
  }
);

You can use toggleClass on hover event

Live Demo

 $(".result").hover(function () {
    $(this).toggleClass("result_hover");
 });
Christen answered 29/5, 2013 at 18:45 Comment(0)
G
12

You could just use: {in and out function callback}

$(".result").hover(function () {
    $(this).toggleClass("result_hover");
 });

For your example, better will be to use CSS pseudo class :hover: {no js/jquery needed}

.result {
    height: 72px;
    width: 100%;
    border: 1px solid #000;
  }
  .result:hover {
    background-color: #000;
  }
Gilbertson answered 29/5, 2013 at 18:45 Comment(0)
S
2

You are missing the dot on the selector, and you can use toggleClass method on jquery:

$(".result").hover(
  function () {
    $(this).toggleClass("result_hover")      
  }
);
Solicitude answered 29/5, 2013 at 18:47 Comment(0)
D
2

Your selector is missing a . and though you say you want to change the border-color - you're adding and removing a class that sets the background-color

Draftee answered 29/5, 2013 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.