Jump to content

Recommended Posts

Hey Guys,

 

I need a bit of help here. I have a little script here running. The javascript part is a rollover that changes the CSS class on hover of the <div> element. The div element and its contents are given from a PHP loop which displays news entries inside the main div.

 

When I hover over any of the news entries, the same one div at the top changes (in its rollover effect). What I want, is a way for each one to change its own, and not eachothers.

 

So it would need to create a new id for each one and work with the javascript somehow.

 

Here's the javascript:

<script type="text/javascript">
function mouseOver()
{
document.getElementById('entry').className = "hoverentry";
}
function mouseOut()
{
document.getElementById('entry').className = "normalentry";
}
</script>

 

and the PHP

<?php
$connect = mysql_connect($host,$user,$pass) or die(mysql_error());
$selectdatabase = mysql_select_db($db) or die(mysql_error());

$getentries = mysql_query("SELECT * FROM newsentries ORDER BY newsid DESC ") or die(mysql_error());

while($filterentries = mysql_fetch_assoc($getentries))
{
$newsid = $filterentries['newsid'];
$title = $filterentries['title'];
$content = substr($filterentries['content'],0,200);
$author = $filterentries['author'];
$date = $filterentries['date'];

echo '<div class="normalentry" id="entry" onMouseOver="mouseOver()" onMouseOut="mouseOut()">';
echo '<h3 class="newsentry">' . "$title  " . "</h3>";
echo '<p class="newsdate">' . "$date"  . "</p> <br />";
echo '<p class="newsentry">' .  "$content" . '......</p>';
echo '<p class="newsposter">Posted By ' . "$author";
?>

 

I have only just started with javascript, that's why I don't know very much.

 

Help would be appreciated.

 

 

You're assigning all the div's the same ID!!  They need to be different ;D

 

<script type="text/javascript">
function mouseOver(id)
{
document.getElementById('entry'+id).className = "hoverentry";
}
function mouseOut(id)
{
document.getElementById('entry'+id).className = "normalentry";
}
</script>

<?php
$i = 0;
while($filterentries = mysql_fetch_assoc($getentries))
{
$i++
$title = $filterentries['title'];
$content = $filterentries['content'];
$author = $filterentries['author'];
$date = $filterentries['date'];

echo '<div id="entry'+$i+'" class="normalentry" onMouseOver="mouseOver('+$i+')" onMouseOut="mouseOut('+$i+')">';
echo '<h3 class="newsentry">' . "$title  " . "</h3>";
echo '<p class="newsdate">' . "$date"  . "</p> <br />";
echo '<p class="newsentry">' .  "$content" . '</p>';
echo '<p class="newsposter">Posted By ' . "$author" . "</p> </div>";
}

 

But, a better thing to do would be just to handle this in the CSS using pseudo:

div.normalentry{
margin-top: 10px;
margin-bottom: 10px;
background-color: #2a2a2a;
background-image: url('scanline.png');
width:570px;
}

div.normalentry:hover{
margin-top: 10px;
margin-bottom: 10px;
background-color: #111111;
background-image: url('scanline.png');
width:570px;
}

 

And please, try and format your code before posting on here  :) Other than that, let me know if either of these solutions work for you. The CSS one is the way to go IMHO. Also, remember that ID's on the same page are unique identifiers, and should only be used once on a single page.

 

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.