Hey again,
I have a problem and I just cannot figure out what is wrong. I want to show news on a page and to fade them in. I am using jQuery AJAX to pull out the news from a MySQL database. Here is the code (I will explain what the exact problem is in the code):
showNews.php - the relevant part
- $row = mysql_fetch_assoc($result);
- while ($row = mysql_fetch_array($result)) {
- echo '<div class="news"><div class="t"><div class="b"><div class="l"><div class="r"><div class="bl"><div class="br"><div class="tl"><div class="tr">
- <h2>' . $row['Title'] . '</h2>' . $row['Time'] . '<p>' . $row['Manchet'] . '</p><br />
-
- <a class="noDecoration" href="#" >» Read more...</a><br /><br /></div></div></div></div></div></div></div></div></div><br />';
- }
The output is as intended.
news.js - imported on
index.php where I want to show the news
- $(function() {
- function fadeInNews() {
- console.log("inside fadeInNews function"); // This is sucessfully displayed in the console
- //$('#mainContent div.news').hide().fadeIn(5000); // When not commented out, this does not work
- // The tests below all output 0, so apparently the problem is finding the divs, which I do not understand since they do appear on the page like <div class="news">text</div>
- console.log($('#mainContent div.news').length);
- console.log($('div.news').length);
- console.log($('.news').length);
- }
-
- function displayNews(startNumber) {
- $.ajax({
- url: 'php/showNews.php',
- type: 'POST',
- data: 'num=' + startNumber, // Don't mind this; it is for coding paging
-
- success: function(result) {
- console.log(result); // The result is tested to be correct (even though only 2 out of 3 news show - but I will deal with that later)
- $('#mainContent').html(result, fadeInNews()); // The news do in fact show, but they do not fade in
- }
- });
- }
- displayNews(0);
- });
index.php contains a <div id="mainContent"></div>.
So, to sum up, selecting the news classes in the fadeInNews function does not work for me. Can anyone see what I did wrong?
Thanks a lot.