It sounds like you need a few things to get this working; overall, you need a way of:
- Detecting that the client has hovered over your item name
- Getting the hidden value client-side (i.e. in the browser)
- Sending the hidden value to the server
- Dealing with the response (e.g. drawing the tooltip)
The html you've posted is pretty close to achieving that, but I'd guess that you will have multiple item links within one page. With that in mind, you could first simplify your markup like so:
<p class="name item-hover-link" data-item-id="12345">
Barman's Shanker
</p>
Now your html contains all the necessary information. However, the version you've posted is also fine, it just contains alot of stuff you don't necessarily need to achieve the particular goal you stated.
Next, we'll use jQuery
to detect if you hover over an item. Note: many web developers will tend use jQuery
rather than raw javascript
, especially for ajax-related stuff. I'm especially lazy, so I'm opting for it also.
Make sure this jQuery/javascript
either comes after your html or is booted in an onLoad event; otherwise, it won't be able to find any ".item-hover-link" elements.
function drawTooltip(data) {
// Google different tooltip drawing implementations
// I'd be amazed if there isn't a 'WoW-style' tooltip
// guide somewhere out there
}
$(".item-hover-link").hover(
function() {
// Mouse entering the link
// $this refers to the <p> that was hovered over
var $this = $(this);
// Check if any previously-loaded item data has been stored
// e.g. they have hovered over it before
var loadedItemData = $this.data("loaded-item-data");
if(loadedItemData !== undefined){
// It's been previously loaded! Use that data to draw
// a tooltip, no need for a server round-trip
drawTooltip(loadedItemData);
}
else {
// It hasn't been loaded before, you need to GET it
// from the server
// Here's where data-item-id="12345" comes in
var id = $this.data("item-id");
var request = "/functions/itemvar.php?id=" + id;
$.get(request, function(data) {
// Your php is generating the response, do whatever
// with it here, I'll assume you didn't touch it.
drawTooltip(data);
// And cache it, to prevent another round trip if they
// re-hover (care for memory leaks though)
$this.data("loaded-item-data", data);
});
}
},
function() {
// Mouse leaving the link, you should use this
// area to remove any initialized stuff while doing
// your round-trip (loading animations, get requests,
// tooltips, etc)
hideTooltip();
}
);
This kind of approach should get you most of the way there. However, you'll need to make sure your back end complies with the request (I'm assuming it's a GET request for tooltip stubs?). Also, you'll need a method for drawing a custom tooltip, much like wowdb
and thottbot
(is that still trendy with the kids these days?). I'd reccomend something like jQueryUI
tooltip
if you want a quick solution or check out the various approaches to tool-tipping in google.