Ben 11

I asked a question related to this issue previously but, I decided that I was not specific enough before.

How exactly would I get Jquery to perform a specific function if it found 2 (or more) images with matching rel attributes?

1 answer

o.k.w 2355
2
points
This was chosen as the best answer

The following example performs a check on mouseover elements with "rel" attribute matching another same one and you can implement your script according to the matching count.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
  $("img[rel='abc']").hover(function(){
    if ($("img[rel='abc']").length > 1) {
        //do something if more than 1
        alert($("img[rel='abc']").length); //alert number of matching elements
    }
    else if($("img[rel='abc']").length == 1) {
        //do something if matches 1
    }
  });
});
</script>
</head>
<body>
<img rel="abc" src="http://doctype.com/images/logo-small.png" alt="one" />
<img rel="abc" src="http://doctype.com/images/logo-small.png" alt="two" />
</body>
</html>
Answered almost 7 years ago by o.k.w