1

In my code below, the JQuery events (mouseenter and leave) are implemented only on the first row of the entire list. So if I have more than one row in the results, the other rows are not effected by the JQuery and no change in the image height is done on mouseover. Can anyone explain the problem? and offer a possible solution?

<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="css/main.css">
    <title></title>
</head>
<body>
<div id="headpanel"></div>
<div id="tablehead">
<a href="addmember.php"><img src="images/ico/add.png" id="addmember"><span style="position:absolute; top:3vmax; left:11vmax;">Add A New Profile </span></a>
</div>
<div id="tablebody">
<?php 
$profile_test = mysqli_query($db,"SELECT * FROM studs WHERE prof_id= '$prof_id'");
while($profile = mysqli_fetch_array($profile_test))
{
    ?>

    <div id="contact<?php echo $profile['stud_id']; ?>" class="contactblock">
<?php   
    echo "Name:";
    echo "&nbsp;";
    echo "&nbsp;";
    echo $profile['firstname'];
    echo "&nbsp;";
    echo "&nbsp;";

    echo $profile['lastname'];
    echo "<br>";
    echo "Marks:";
    echo "&nbsp;";
    echo $profile['marks'];
?>

 <img src="images/ico/edit.png"  id="editprofile">

</div>
<?php
}
?>
</div>

</body>
</html>
<script type="text/javascript">
$(document).ready(function(){

    $("#editprofile").mouseenter(function(){

        $(this).animate({"height":"4vmax"});
    })
    $("#editprofile").mouseleave(function(){

        $(this).animate({"height":"3vmax"});
    })

})
</script>
2
  • where is $db defined? Commented Jul 12, 2016 at 19:58
  • Its there I didn't provide the upper PHP part! Commented Jul 12, 2016 at 20:02

1 Answer 1

2

You're using the same ID on multiple elements. Inside your loop, you have:

<img src="images/ico/edit.png"  id="editprofile">

IDs must be unique:

The Element.id property represents the element's identifier, reflecting the id global attribute. It must be unique in a document...

Change that to a class:

<img src="images/ico/edit.png"  class="editprofile">

And then adjust your binding to $(".editprofile") instead of $("#editprofile")

Sign up to request clarification or add additional context in comments.

1 Comment

It solved it! Thanks a Ton! :D ... I got to learn a new thing too! .. didn't know this constraint about IDs, always wanted to know the advantage of class over ID , Now I Know!

Your Answer

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.