Using Jquery to create a hover effect on a parent div that affects two of its grandchildren and one of its children

Hey folks, I'm looking to translate this CSS hover effect into jQuery code.

Below is the CSS version for hovering:
index.html

<div class="container-fluid">
      <div class="row why-us-box">
        <div class="why-item col-xs-12 col-md-4 col-sm-4 text-center">
          <div class="why-box-head">
            <i class="fa fa-group"></i>
            <h4>
              <b>Reason 1</b>
            </h4>
          </div>
          <p class="text-center">
            Detail Reason 1
          </p>
        </div>

        <div class="why-item col-xs-12 col-md-4 col-sm-4 text-center">
          <div class="why-box-head">
            <i class="fa fa-trophy"></i>
            <h4>
              <b>Reason 2</b>
            </h4>
          </div>
          <p class="text-center">
            Detail Reason 2
          </p>
        </div>

        <div class="why-item col-xs-12 col-md-4 col-sm-4 text-center">
          <div class="why-box-head">
            <i class="fa fa-graduation-cap"></i>
            <h4>
              <b>Reason 3</b>
            </h4>
          </div>
          <p class="text-center">
            Detail Reason 3
          </p>
        </div>
      </div><!-- End of why-us-box -->
    </div> <!-- End of container-fluid -->

style.css

.why-item:hover .why-box-head i {
    -webkit-transform:scale(1.4);
    transform:scale(1.4);
}

.why-item:hover .why-box-head h4 {
  -webkit-transform:scale(1.1);
  transform:scale(1.1);
}

.why-item:hover p {
  -webkit-transform:scale(1.1);
  transform:scale(1.1);
}

.why-box-head i {
  color: #607D8B;
  font-size: 70px;
  display: inline-block;
  line-height: 40px;
  margin-bottom: 30px;
  -webkit-transition: all 0.7s ease;
  transition: all 0.7s ease;
}

.why-box-head h4 {
  font-size: 18px;
    line-height: 1.1;
    font-weight: bold;
  -webkit-transition: all 0.7s ease;
  transition: all 0.7s ease;
}

.why-item p {
    line-height: 22px;
    color: #797979;
    font-size: 14px;
    margin-bottom: 20px;
  -webkit-transition: all 0.7s ease;
  transition: all 0.7s ease;
}


It's been working great with CSS, but now I want to experiment with jQuery.

So, how can I target the two grandchildren and a child when the parent div (why-item div) is hovered over? I aim to apply effects to:
the i tag which is a child of why-box-head, itself a child of why-item (the grandchild)
the h3 tag which is a child of why-box-head, again a child of why-item (the grandchild)
the p tag which is simply a child of why-item (the child)

I attempted this jQuery snippet to affect the i tag, but handling three tags simultaneously seems quite puzzling to me.

$(".why-item").hover(function(){
    $("why-box-head i").css({"-webkit-transform":"scale(1.4)","transform":"scale(1.4)"});
    }, function(){
    $("why-box-head i").css({"-webkit-transition":"all 0.7s ease","transition":"all 0.7s ease"});
});

Answer №1

Utilize the code $(this).find('.why-box-head h4') to locate the corresponding grandchild within the element currently being hovered over. Also, don't forget to include the p in the selector.

$(".why-item").hover(
  function() {
    $(this)
      .find(".why-box-head h4, p")
      .css({ "-webkit-transform": "scale(1.1)", transform: "scale(1.1)" })
      .end()
      .find('.why-box-head i')
      .css({'-webkit-transform':'scale(1.4)',
    'transform':'scale(1.4)'});
  },
  function() {
    $(this)
      .find(".why-box-head h4, p")
      .css({ "-webkit-transform": "scale(1)", transform: "scale(1)" })
      .end()
      .find('.why-box-head i')
      .css({'-webkit-transform':'scale(1)',
    'transform':'scale(1)'});
  }
);
.why-box-head i {
  color: #607D8B;
  font-size: 70px;
  display: inline-block;
  line-height: 40px;
  margin-bottom: 30px;
  -webkit-transition: all 0.7s ease;
  transition: all 0.7s ease;
}

.why-box-head h4 {
  font-size: 18px;
    line-height: 1.1;
    font-weight: bold;
  -webkit-transition: all 0.7s ease;
  transition: all 0.7s ease;
}

.why-item p {
    line-height: 22px;
    color: #797979;
    font-size: 14px;
    margin-bottom: 20px;
  -webkit-transition: all 0.7s ease;
  transition: all 0.7s ease;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<div class="container-fluid">
  <div class="row why-us-box">
    <div class="why-item col-xs-12 col-md-4 col-sm-4 text-center">
      <div class="why-box-head">
        <i class="fa fa-group"></i>
        <h4>
              <b>Reason 1</b>
            </h4>
      </div>
      <p class="text-center">
        Detail Reason 1
      </p>
    </div>

    <div class="why-item col-xs-12 col-md-4 col-sm-4 text-center">
      <div class="why-box-head">
        <i class="fa fa-trophy"></i>
        <h4>
              <b>Reason 2</b>
            </h4>
      </div>
      <p class="text-center">
        Detail Reason 2
      </p>
    </div>

    <div class="why-item col-xs-12 col-md-4 col-sm-4 text-center">
      <div class="why-box-head">
        <i class="fa fa-graduation-cap"></i>
        <h4>
              <b>Reason 3</b>
            </h4>
      </div>
      <p class="text-center">
        Detail Reason 3
      </p>
    </div>
  </div>
  <!-- End of why-us-box -->
</div>
<!-- End of container-fluid -->

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

display a div beside the anchor element using jQuery

I could really use some assistance. I'm fairly new to jquery and I'm encountering an issue that appears to be quite simple. I apologize if this has been asked before, but I couldn't find the solution I needed. My problem involves having 2 l ...

Utilize CSS and Bootstrap to ensure equal-sized responsive images in a row by scaling them down to the same dimensions

I am new to web design and struggling with resizing images using the img-fluid class in a particular portfolio view where they function as thumbnails. I will upload some images to clarify my goal. The challenge is maintaining consistency in size for each i ...

Stop the bootstrap navbar from resizing

I implemented a bootstrap navbar that looks like this... <nav class="navbar navbar-inverse"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="#">WebSiteName</a> </div& ...

Find and target all CSS elements that have the property of "display: inline

Can the CSS be searched by property:value instead of selector/attribute? Or does this require parsing through a server script? If it is achievable, I am considering developing a script that will automatically include the IE7 hack when the selector contain ...

I am interested in running JavaScript code on a page that has been loaded through AJAX

I am struggling to execute the Javascript loaded within an ajax page. Here is the link to my loaded ajax page: https://jsfiddle.net/4dsbry7j/ JS : var xmlhttp = new XMLHttpRequest(); var url = "http://localhost/ajax/find2.php"; xmlhttp.onreadystatechang ...

Surprising behavior of translateZ in Framer Motion

Trying to position elements in a circular layout has led to unexpected behavior when using framer motion's translateZ. By applying styles with a template literal without shorthand for transforms, the desired effect is achieved: transform: ` rotateY( ...

Eliminating unnecessary white space while utilizing the first-letter CSS property

Is there a way to remove the extra whitespace added under the first line when making the first letter of a paragraph bigger than the rest of the text? I already tried adjusting padding and margins on p::first-line without success. p{ line-height: 1.4; } p ...

What is the process for transforming the outcome of a Javascript function into a webpage containing solely a JSON string?

I have a specific requirement where I need to fetch a URL and ensure that the only content displayed on the page is a JSON string. For instance, let's say I created a basic function called getDayOfWeek() to determine the current day of the week. The ...

The webpage continues to refresh after executing a JavaScript function triggered by the AJAX response

I have experimented with various solutions for calling a JavaScript function returned from an AJAX response. While each method worked to some extent, I found that using an alert within the refreshResults function was necessary in order to display the resul ...

Explore the tree structure with AngularJS and populate it with data from a JSON

After utilizing the AngularJS TreeView example from this JSFiddle link, I encountered a peculiar issue. When attempting to retrieve data from a JSON file similar to the code provided, the tree structure appeared empty. Upon inspecting the $scope.roleList1 ...

Is it possible for a PHP form to generate new values based on user input after being submitted?

After a user fills out and submits a form, their inputs are sent over using POST to a specified .php page. The question arises: can buttons or radio checks on the same page perform different operations on those inputs depending on which one is clicked? It ...

Aligning CSS border headers in a slanted or rotated table cell format

Is there a way to create slanted or rotated table headers in HTML? How can I ensure that the slanted inner-side header border aligns perfectly with the vertical border of the table cell element? Here is an example of the HTML code: <table> < ...

Create dynamic cells for CSS grid using JavaScript

I have been manually generating grid cells in a specific pattern by copying and adjusting <div> elements. Although this method works, I am interested in creating an algorithm that can automatically generate the desired layout. The left box in the exa ...

"Apply a class to a span element using the onClick event handler in JavaScript

After tirelessly searching for a solution, I came across some answers that didn't quite fit my needs. I have multiple <span id="same-id-for-all-spans"></span> elements, each containing an <img> element. Now, I want to create a print ...

Loop through a list of items and apply the bootstrap-select plugin to each

In an attempt to incorporate an update button within a loop using bootstrap-selectpicker, I encountered a challenge. Within each iteration of the loop, there is a form containing multiple select elements with selectpicker and a hidden button to save the se ...

Tips for customizing the CSS styling of the validation ng-class error

Seeking advice: Is there a way to customize the CSS style of the 'error' validation error for ng-class in Bootstrap v3? This is my current code: ng-class="(form.datos.$invalid) && ( form.datos.$touched || submitted) ? 'error&apos ...

Unable to load data from server using AJAX in jQuery datatables

Hello there, I am seeking assistance regarding DataTables. I have been using it for a while now through both dom manipulation and JSON code. However, I am facing issues with two large result sets that are running too slow. In an attempt to resolve this, I ...

Is there a way to eliminate a div and all its contents from the DOM?

As I work on my web application, I am trying to implement a system where error messages can be returned via ajax upon success or failure. The ajax script is functioning correctly in terms of returning errors or successes. However, my challenge lies in com ...

Upgrading the grayscale filter to 100% in Internet Explorer 11

Recently, I learned that the support for the grayscale(100%) filter was eliminated in Internet Explorer 9 and later versions. Can you suggest an alternative filter that I can use for .svg format images instead? ...

Trouble with Div Display

I have a section below my form that will show an alert message if there is an error. The issue I am facing is that if I use the inline-block property, everything within the section will be displayed in the alert color. I specifically want the background- ...