Akenatehm Posted March 15, 2009 Share Posted March 15, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/149454-integrating-php-and-javascript-for-rollover/ Share on other sites More sharing options...
shlumph Posted March 15, 2009 Share Posted March 15, 2009 You're assigning all the div's the same ID!! They need to be different <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. Quote Link to comment https://forums.phpfreaks.com/topic/149454-integrating-php-and-javascript-for-rollover/#findComment-784947 Share on other sites More sharing options...
Akenatehm Posted March 15, 2009 Author Share Posted March 15, 2009 the CSS way did not work. What is the other way I can do it? Quote Link to comment https://forums.phpfreaks.com/topic/149454-integrating-php-and-javascript-for-rollover/#findComment-784949 Share on other sites More sharing options...
shlumph Posted March 15, 2009 Share Posted March 15, 2009 I give up Quote Link to comment https://forums.phpfreaks.com/topic/149454-integrating-php-and-javascript-for-rollover/#findComment-784951 Share on other sites More sharing options...
Akenatehm Posted March 15, 2009 Author Share Posted March 15, 2009 Anybody else then? Quote Link to comment https://forums.phpfreaks.com/topic/149454-integrating-php-and-javascript-for-rollover/#findComment-784955 Share on other sites More sharing options...
shlumph Posted March 15, 2009 Share Posted March 15, 2009 I don't think they gave you a chance Double post: http://www.phpfreaks.com/forums/index.php/topic,243091.msg1135176.html (notice quote in my first post) Quote Link to comment https://forums.phpfreaks.com/topic/149454-integrating-php-and-javascript-for-rollover/#findComment-784963 Share on other sites More sharing options...
Akenatehm Posted March 15, 2009 Author Share Posted March 15, 2009 Anyone else? Btw. This is now a PHP/Javascript related issue not CSS anymore. Quote Link to comment https://forums.phpfreaks.com/topic/149454-integrating-php-and-javascript-for-rollover/#findComment-784989 Share on other sites More sharing options...
.josh Posted March 15, 2009 Share Posted March 15, 2009 + is javascript . is php Quote Link to comment https://forums.phpfreaks.com/topic/149454-integrating-php-and-javascript-for-rollover/#findComment-784993 Share on other sites More sharing options...
Akenatehm Posted March 15, 2009 Author Share Posted March 15, 2009 + is javascript . is php I don't quite get what you're trying to say to me?? Quote Link to comment https://forums.phpfreaks.com/topic/149454-integrating-php-and-javascript-for-rollover/#findComment-785009 Share on other sites More sharing options...
shlumph Posted March 15, 2009 Share Posted March 15, 2009 Perhaps you should go to barnes and nobles, and pick up a book or two, then. Quote Link to comment https://forums.phpfreaks.com/topic/149454-integrating-php-and-javascript-for-rollover/#findComment-785010 Share on other sites More sharing options...
.josh Posted March 15, 2009 Share Posted March 15, 2009 Detectives usually start their investigation at the crime scene. Quote Link to comment https://forums.phpfreaks.com/topic/149454-integrating-php-and-javascript-for-rollover/#findComment-785013 Share on other sites More sharing options...
Akenatehm Posted March 15, 2009 Author Share Posted March 15, 2009 Lol thats a good one Crayon. Could you tell me what you meant? Quote Link to comment https://forums.phpfreaks.com/topic/149454-integrating-php-and-javascript-for-rollover/#findComment-785022 Share on other sites More sharing options...
shlumph Posted March 15, 2009 Share Posted March 15, 2009 He meant that you shouldn't go to barnes and nobles, and should figure this out in the code. I, personally, think think otherwise IMHO. Quote Link to comment https://forums.phpfreaks.com/topic/149454-integrating-php-and-javascript-for-rollover/#findComment-785025 Share on other sites More sharing options...
.josh Posted March 15, 2009 Share Posted March 15, 2009 What do you usually do with a puzzle piece when trying to figure out where it goes? Quote Link to comment https://forums.phpfreaks.com/topic/149454-integrating-php-and-javascript-for-rollover/#findComment-785028 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.