473,385 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Confused using getElementsByTagName

hey everyone,
so ive been going around the internet all day trying to help guide me in creating a div that rotates content every 4 seconds using ajax and ive decided a forum post might be the most effective at this point. my js script code below is obviously and the current problem im having is figuring out how to build the array to store the information of each of the hidden divs (class = "headline").

Expand|Select|Wrap|Line Numbers
  1. // Rotating headlines
  2.  
  3.  
  4. function headlineRotate ( )
  5. {
  6.     var HeadArray=new Array()
  7.     var numofHeads=document.getElementsByTagName("div");
  8.     var x = 0
  9.  
  10.     for (var i=0; i<numofHeads; i++){
  11.         if (document.className=="headline"){
  12.             HeadArray.push(alldivs[i])
  13.         }
  14.     }  
  15.   if (x<3){
  16.       x++;
  17.   }
  18.   else{
  19.       x=0;
  20.   }
  21.   document.getElementById('currentHeadline').innerHTML = numofHeads[3].value;
  22. }
  23.  
  24. function rotater() {
  25.     setInterval ( "headlineRotate()", 4000);
  26. }
Sep 19 '07 #1
10 2086
gits
5,390 Expert Mod 4TB
hi ...

welcome to TSDN ...

as far as i can see ... the script wouldn't work in any way ... sorry that i have to say that ... but it doesn't matter ... lets try from the beginning, with an analysis of the task you have ... please show the html you are using and tell us how you fill the hidden? divs with content. then tell us exactly what your 'rotator'-function should do ... may be you don't need 4 divs ... simply one that is updated with content that we might get from a list (object that was filled instead of 4 divs) ...

kind regards ...
Sep 19 '07 #2
This would be the block of code Im working from. My plan was to have the getElementsByTagName load all my divs then from there select the ones in the class of "headline". Into an array via the for loop. From there display them one after another. My main question was how to get them loaded into that array but if you have a better suggestion on how to achieve what I'm looking to do here please fill me in (obviously Im somewhat new to js).

Expand|Select|Wrap|Line Numbers
  1. //Top 5 headlines from the different sections
  2. echo "<div class=\"headline\" style=\"background:#e9e9e9\">";
  3. echo "<div align=\"left\" style=\"background: url(topstoriesbg.jpg) center center repeat-x\"><img src=\"politicsthumb.gif\"/>&nbsp;&nbsp;<a href=\"http://www.thenicheplayers.com/news/politics.php\" class=\"topStoryHeader\">Politics</a></div><br />";
  4. echo "<img src=\"images/".$political['img']."\" width=\"160\" height=\"175\" style=\"float: left; margin: 0 10px 5px; border: 4px; border-color: #5F7E93; border-style: inset\" />";
  5. echo "<br style=\"clear: left\" />";
  6. echo "<a href=\"news.php?news_id=".$political['filename']."\" class=\"topStory\">".$political['title']."</a>";
  7. echo "<br style=\"clear: left\" />";
  8. echo "</div>";
  9.  
  10. echo "<div class=\"headline\" style=\"background:#e9e9e9\">";
  11. echo "<div align=\"left\" style=\"background: url(topstoriesbg.jpg) center center repeat-x\"><img src=\"entertainmentthumb.gif\"/>&nbsp;&nbsp;<a href=\"http://www.thenicheplayers.com/news/entertainment.php\" class=\"topStoryHeader\">Entertainment</a></div><br />";
  12. echo "<img src=\"images/".$entertainment['img']."\" width=\"160\" height=\"175\" style=\"float: left; margin: 0 10px 5px; border: 4px; border-color: #5F7E93; border-style: inset\" />";
  13. echo "<br style=\"clear: left\" />";
  14. echo "<a href=\"news.php?news_id=".$entertainment['filename']."\" class=\"topStory\">".$entertainment['title']."</a>";
  15. echo "<br style=\"clear: left\" />";
  16. echo "</div>";
  17.  
  18. echo "<div class=\"headline\" style=\"background:#e9e9e9\">";
  19. echo "<div align=\"left\" style=\"background: url(topstoriesbg.jpg) center center repeat-x\"><img src=\"sportsthumb.gif\"/>&nbsp;&nbsp;<a href=\"http://www.thenicheplayers.com/news/sports.php\" class=\"topStoryHeader\">Sports</a></div><br />";
  20. echo "<img src=\"images/".$sports['img']."\" width=\"160\" height=\"175\" style=\"float: left; margin: 0 10px 5px; border: 4px; border-color: #5F7E93; border-style: inset\" />";
  21. echo "<br style=\"clear: left\" />";
  22. echo "<a href=\"news.php?news_id=".$sports['filename']."\" class=\"topStory\">".$sports['title']."</a>";
  23. echo "<br style=\"clear: left\" />";
  24. echo "</div>";
  25.  
  26. echo "<div class=\"headline\" name=\"headline\" style=\"background:#e9e9e9\">";
  27. echo "<div align=\"left\" style=\"background: url(topstoriesbg.jpg) center center repeat-x\"><img src=\"businessthumb.gif\"/>&nbsp;&nbsp;<a href=\"http://www.thenicheplayers.com/news/business.php\" class=\"topStoryHeader\">Business</a></div><br />";
  28. echo "<img src=\"images/".$business['img']."\" width=\"160\" height=\"175\" style=\"float: left; margin: 0 10px 5px; border: 4px; border-color: #5F7E93; border-style: inset\" />";
  29. echo "<br style=\"clear: left\" />";
  30. echo "<a href=\"news.php?news_id=".$business['filename']."\" class=\"topStory\">".$business['title']."</a>";
  31. echo "<br style=\"clear: left\" />";
  32. echo "</div>";
  33.  
  34. echo "<div class=\"headline\" name=\"headline\" style=\"background:#e9e9e9\">";
  35. echo "<div align=\"left\" style=\"background: url(topstoriesbg.jpg) center center repeat-x\"><img src=\"miscthumb.gif\"/>&nbsp;&nbsp;<a href=\"http://www.thenicheplayers.com/news/misc.php\" class=\"topStoryHeader\">Miscellaneous</a></div><br />";
  36. echo "<img src=\"images/".$misc['img']."\" width=\"160\" height=\"175\" style=\"float: left; margin: 0 10px 5px; border: 4px; border-color: #5F7E93; border-style: inset\" />";
  37. echo "<br style=\"clear: left\" />";
  38. echo "<a href=\"news.php?news_id=".$misc['filename']."\" class=\"topStory\">".$misc['title']."</a>";
  39. echo "</div>";
  40. echo "</div>";
  41. mysql_close($con);
  42. ?>
  43. <div class="pagination" id="currentHeadline">This sucks!</div>
  44. <p>
  45. <script type="text/javascript">
  46. rotater()
  47. </script>
  48. </p>
Sep 19 '07 #3
gits
5,390 Expert Mod 4TB
hmmm ...

i don't see any ajax-code there ... but let me summarize. you have some divs and fill them in php with the current content. i assume they are hidden and all of them have the className 'headline' assigned? ... now you want to display one of them every 4 seconds ...ok? there is no update of the content of the divs during site lifecycle or is it? so when its not and i'm right ... your idea wasn't that bad ... how do you hide the divs - is it declared in the 'headline'-class?

kind regards
Sep 19 '07 #4
Correct, sorry for my inability to provide sufficient information today..I'm kinda all over the place with a few different things. The information in the mySQL database only gets updated daily so the divs do not need to be updated during the page life cycle. The "headline" class divs' display is set to hidden.
Sep 19 '07 #5
gits
5,390 Expert Mod 4TB
hi ...

ok :) so lets start:

we create global vars and some functions:

Expand|Select|Wrap|Line Numbers
  1. var headlines = [];
  2. var idx = 0;
  3.  
  4. function get_headline_containers() {
  5.     var all_divs = document.getElementsByTagName('div');
  6.     var hdl_divs = [];
  7.  
  8.     for (var i = 0; i < all_divs.length; i++) {
  9.         var div = all_divs[i];
  10.  
  11.         if (div.className == 'headline') {
  12.             hdl_divs.push(div);
  13.         }
  14.     }
  15.  
  16.     return hdl_divs;
  17. }
  18.  
  19. function display_headline(idx) {
  20.     for (var i = 0; i < headlines.length; i++) {
  21.         headlines[i].style.display = i == idx ? 'block' : 'none';
  22.     }
  23. }
  24.  
  25. function rotate_headlines() {
  26.     idx++;
  27.  
  28.     if (idx > headlines.length) {
  29.         idx = 0;
  30.     }
  31.  
  32.     window.setTimeout(function() {
  33.         display_headline(idx);
  34.     }, 4000);
  35. }
  36.  
  37. function init_page() {
  38.     headlines = get_headline_containers();
  39.     display_headline(idx);
  40.     rotate_headlines();
  41. }
  42.  
in body's onload you call 'init_page()' ...

kind regards
Sep 20 '07 #6
Alright its almost working perfectly except it only rotates once (just from the first headline to the second) then stops. I tried switching it to setInterval instead of setTimeout but that didn't fix it. The only thing I didn't do was put it in the body onload because this is a PHP include file so the body tag is not present in this block of code.
Sep 20 '07 #7
gits
5,390 Expert Mod 4TB
arrgh ... my bad ... have a look:

Expand|Select|Wrap|Line Numbers
  1. function rotate_headlines() {
  2.     idx++;
  3.  
  4.     if (idx > headlines.length) {
  5.         idx = 0;
  6.     }
  7.  
  8.     window.setTimeout(function() {
  9.         display_headline(idx);
  10.         rotate_headlines();
  11.     }, 4000);
  12. }
  13.  
kind regards
Sep 20 '07 #8
gits
5,390 Expert Mod 4TB
and a little bit better ;)

Expand|Select|Wrap|Line Numbers
  1. function rotate_headlines() {
  2.     idx++;
  3.  
  4.     if (idx > headlines.length) {
  5.         idx = 0;
  6.     }
  7.  
  8.     display_headline(idx);
  9.  
  10.     window.setTimeout(function() {
  11.         rotate_headlines();
  12.     }, 4000);
  13. }
  14.  
  15. function init_page() {
  16.     headlines = get_headline_containers();
  17.     rotate_headlines();
  18. }
  19.  
Sep 20 '07 #9
Thanks! It works beautifully I made to small adjustments 1) intial value for idx = -1 and 2)(idx >= because it started on the second div and showed a blank one at the end. Thanks again though, you saved me alot of time and frustration!
Sep 21 '07 #10
gits
5,390 Expert Mod 4TB
hi ...

of course :) ... i forgot that after changing the initial call ...

glad to hear you go it working ... post back to the forum anytime you have more questions ...

kind regards
Sep 21 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: adam | last post by:
i'm working on a portion of a CMS that allows content-admins to browse a product list, and add individual products into the taxonomy by clicking checkboxes next to categories they might belong in....
3
by: Andy | last post by:
Hello, I have the following example XML: <data> <package> <packageid>123</packageid> <package_article> <articleid>article1</articleid> </package_article> </package>
2
by: Scamjunk | last post by:
I have been desperately looking for a treeview-type solution for my problem for the past three weeks and have been greatly unsuccessful. I am totally new to the world of XSLT and I *don't know*...
9
by: retrofill | last post by:
Hey i've got a problem with printing out an XML document: XML doc: http://www.deniseburrows.pwp.blueyonder.co.uk/javascript/data.xml If i remove the comments from the lines that add the...
5
by: subbulakshmi | last post by:
hi how to read data from XMLDocument by using Javascript. i got coding from internet. but its not read data. help me its in JavaScript ----------------------- //server return XML...
2
by: ysuwardy | last post by:
Hello everyone, I have a question on XML with Javascript. Here is my situation: I have two xml documents (1.xml and 2.xml) with similar structure: <person> <data> <value>1</value>...
2
by: davidson1 | last post by:
Hai friends..for menu to use in my website..i found in one website....pl look below website.... http://www.dynamicdrive.com/dynamicindex1/omnislide/index.htm i downloaded 2 files.... ...
4
by: Ouray Viney | last post by:
Xml <ib>8.4.27.5</ib> python from xml.dom import minidom xmldoc = minidom.parse('C:\TestProfile.xml') xmldoc
0
by: sujata321 | last post by:
Hi, I need to update an XML file using ASP and javascript/vbscript. But this is not working.It is showing the error – ‘document’ is not defined –in the line ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.