0
\$\begingroup\$

I am using this code to add a class on mouse hover to a link button and remove it on mouse leave.

jQuery(".banner-wrap__color-1 > .banner-wrap__inner > .banner-wrap__desc > .banner-btn > .btn-link").hover(function () {
 jQuery(".banner-wrap__color-1 > .banner-wrap__inner").toggleClass("active");
 jQuery(".banner-wrap__color-1").toggleClass("active");
});
jQuery(".banner-wrap__color-2 > .banner-wrap__inner > .banner-wrap__desc > .banner-btn > .btn-link").hover(function () {
 jQuery(".banner-wrap__color-2").toggleClass("active");
});
jQuery(".banner-wrap__color-3 > .banner-wrap__inner > .banner-wrap__desc > .banner-btn > .btn-link").hover(function () {
 jQuery(".banner-wrap__color-3").toggleClass("active");
});
jQuery(".btn-align > .btn-inverse").hover(function () {
 jQuery(".service-box__style-1").toggleClass("active");
});
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jul 30, 2015 at 3:36
\$\endgroup\$
2
  • \$\begingroup\$ Are you sure you don't just need some CSS hover rules? \$\endgroup\$ Commented Jul 30, 2015 at 13:36
  • 1
    \$\begingroup\$ We could give you better advice if you also included your HTML so that we can understand what you are really trying to accomplish. (The generated HTML will do; we probably don't need to see the WordPress code that produces the HTML.) \$\endgroup\$ Commented Jul 31, 2015 at 4:08

2 Answers 2

1
\$\begingroup\$

Is hard to evaluate jquery without the DOM when you are making selectors.

You are using a lot of > check if you can use .find.

In a initial, state, theres a few lines repeated. that can be extracted in a outside function.

function toggleClass($container) {
 $container.find(".banner-wrap__desc > .banner-btn > .btn-link").hover(function () {
 $container.toggleClass("active"); 
 })
 }
toggleClass(jQuery(".banner-wrap__color-1"));
toggleClass(jQuery(".banner-wrap__color-2"));
toggleClass(jQuery(".banner-wrap__color-3"));

And the full code: http://jsfiddle.net/ja5py40o/

answered Jul 30, 2015 at 13:04
\$\endgroup\$
1
  • \$\begingroup\$ I have try this code and its working! Thank you for the help! \$\endgroup\$ Commented Aug 5, 2015 at 6:15
1
\$\begingroup\$

You could store in a variable so that the Dom doesn't need to be traversed several times:

var myvar = jQuery(".banner-wrap__color-1 > .banner-wrap__inner > .banner-wrap__desc > .banner-btn > .btn-link");

Then just use myvar instead of retrieving it again... A

answered Jul 30, 2015 at 13:48
\$\endgroup\$
4
  • \$\begingroup\$ He doesn't query the same object more than once. The top level object is numbered 1,2, and 3 \$\endgroup\$ Commented Jul 30, 2015 at 15:14
  • \$\begingroup\$ couldn't he get the parent node, then get the children when needed? \$\endgroup\$ Commented Jul 30, 2015 at 15:20
  • \$\begingroup\$ I think the hover function should be assigned to .btn-link then let the parents be grabbed as needed. ...but .banner-wrap__color-1 is an oddball and has more being done. And I kinda want to see the html behind this, for a better assessment. \$\endgroup\$ Commented Jul 30, 2015 at 16:01
  • \$\begingroup\$ jsfiddle.net/yrralej/qgLxa16r/1 please refer to this link for my complete code. ThankYou! \$\endgroup\$ Commented Aug 5, 2015 at 6:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.