Jump to content

Access another div from 'this'


rondog

Recommended Posts

I am trying to make play button appear on top of a thumbnail when you mouse over it. Just like the thumbs on www.hulu.com. The code I have works for the first thumbnail. Any other thumbnail I mouse over, the first thumbnail's play button appears.

 

I somehow need to use 'this' to get the next element in the div. 'playbtn' is the element I need to get at for each thumbnail. Here is my code:

 

the JS:

<script type="text/javascript">
function hoveron(item)
{
document.getElementById("playbtn").style.display = 'inline';
}
function hoveroff(item)
{
document.getElementById("playbtn").style.display = 'none';
}
</script>

 

the HTML:

<table width="800" cellpadding="5" cellspacing="20" border="0">
  <tr>
    <td class="courseTable" valign="top" width="33%">
    <div>
        <a href="somesite.com">
            <img id="thumb" border="0" src="thumb.jpg" onmouseover="hoveron(this);" onmouseout="hoveroff(this);"/>
            <div id="playbtn" style="position:relative; top: -20px; left: -90px; z-index:5; display:none;"><img border="0" src="images/btn-play-big.png" /></div>
        </a>
    </div>
    </td>
    <td class="courseTable" valign="top" width="33%">
    <div>
        <a href="somesite.com">
            <img id="thumb" border="0" src="thumb.jpg" onmouseover="hoveron(this);" onmouseout="hoveroff(this);"/>
            <div id="playbtn" style="position:relative; top: -20px; left: -90px; z-index:5; display:none;"><img border="0" src="images/btn-play-big.png" /></div>
        </a>
    </div>
    <td class="courseTable" valign="top" width="33%">
    <div>
        <a href="somesite.com">
            <img id="thumb" border="0" src="thumb.jpg" onmouseover="hoveron(this);" onmouseout="hoveroff(this);"/>
            <div id="playbtn" style="position:relative; top: -20px; left: -90px; z-index:5; display:none;"><img border="0" src="images/btn-play-big.png" /></div>
        </a>
    </div>
    </td>
  </tr>
</table>

Link to comment
Share on other sites

If you are loading it from a database, just append the row ID (assuming there is one) to the element ID. Like so:

 

<img id="thumb_123" border="0" src="thumb.jpg" onmouseover="hoveron(123);" onmouseout="hoveroff(123);"/>
<div id="playbtn_123" style="position:relative; top: -20px; left: -90px; z-index:5; display:none;"><img border="0" src="images/btn-play-big.png" /></div>

 

<script type="text/javascript">
function hoveron(item)
{
   document.getElementById("playbtn_"+item).style.display = 'inline';
}
function hoveroff(item)
{
   document.getElementById("playbtn_"+item).style.display = 'none';
}
</script>

 

The rule is that element IDs must be unique. So if you're not loading from a database, just make sure they are. If you wish to use this from the <img> tag, you would need something like this.nextSibling in order to refer to the <div>. It may however, just refer to the space between the two elements. I recommend that you have no line breaks, tabs, spaces or anything else between them.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.