0

tooltip dont work with while loop all the icons show me the info of first icon only here is the php

$check = odbc_exec($conn,"select * from Item");
while($r = odbc_fetch_array($check)){
$info = $r['Info'];
$Image = $r['image'];
echo  "<span class='hint'><img src='$Image'></span>
<div class='readitem' style='display:none'>$info</div>";

}

jquery : tooltip

<script type="text/javascript">
$(document).ready(function() {
    var changeTooltipPosition = function(event) {
      var tooltipX = event.pageX - 10;
      var tooltipY = event.pageY + 10;
      $('div.tooltip').css({top: tooltipY, left: tooltipX});};
    var showTooltip = function(event) {
    var item = $('.readitem').html();

      $('<div class="tooltip"><center>'+ item + '</center></div>').appendTo('body');
      changeTooltipPosition(event);
    };
    var hideTooltip = function() {
       $('div.tooltip').remove();
    };
    $(".hint").bind({
       mousemove : changeTooltipPosition,
       mouseenter : showTooltip,
       mouseleave: hideTooltip
    });
});

</script>

any solution ?

Thank You ..

2 Answers 2

1

I have never seen a while loop without a condition. Your while loop should be formed like

while (condition) {
    //your code
}

where condition is an expression evaluated as true or false. However in your case, you may have an array of icon URL. You should use a for loop which is more convenient to loop in an array.

for ($i = 0; $i < count($images); $i++) {
    echo $images[$i]; // Assuming $images is an array.  
}
Sign up to request clarification or add additional context in comments.

1 Comment

i have added the orginal while loop i using
0

It's a problem with the var item = $('.readitem').html(); line.

When you do $('.readitem'), it will match all elements on the page which have that class. When then calling .html() on that, it'll return the html of the first matched element (thus always showing you the first one).

What you'd instead need to do, is look within the .hint on which you're hovering, in order to find the sibling .readitem, and then use the html of that one.

So, this should work instead:

var item = $(this).siblings('.readitem').html();

$(this) should be the instance of the .hint on which you are hovering, so you're trying to find the .readitem in the sibling elements of it.

5 Comments

Can you include in your original post the whole while loop?
i have added the orginal while loop i using
Hey DragoN, I've edited my answer now that I've understood the problem! I think that should fix it for you!
thx , but with find it give me Null error, so i used $(this).siblings
Ah, you're right, I misread the html. I thought the .readitem was within .hint, but they are at the same level. I'll update my answer to have the correct code!

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.