-1

I would like to know if you can actually concatenate class's/ID's in jQuery to DRY more effectively.

like this:

$(".div1").hover(function() {
        $(".title").hide();
    });

$(".div2").hover(function() {
        $(".title").hide();
    });

to this:

$(".div1 + .div2").hover(function() {
        $(".title").hide();
    });

Is there a way of doing that?

Thank you

chicken burger
  • 774
  • 2
  • 12
  • 32
  • 1
    Using `+` will select all `.div2` elements preceded by a `.div1` element. https://api.jquery.com/next-adjacent-selector/ – inarilo May 20 '17 at 16:18

3 Answers3

2
$(".div1, .div2").hover(function() {

This will fix

Vishnu
  • 127
  • 6
  • Thank you very much for your help, I gave you one point, but I accepter the answer of the solution above yours to give help for newbies. thank you for your time :) – chicken burger May 20 '17 at 20:07
2
Try This
    $(".div1, .div2").hover(function() {
            $(".title").hide();
     });
jvk
  • 2,133
  • 3
  • 19
  • 28
  • thank you for your help and answer, I gave you one point,but I accepted the answer below to help newbies on the website, but thank you very much for your time :) – chicken burger May 20 '17 at 20:06
2

you can select more than one element by separating each element with a comma.

$(".div1, .div2").hover(function() {
   $(".title").hide();
});
Mouad DB
  • 36
  • 2