Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-24-2012, 09:17 PM   PM User | #1
cloudstryphe
New Coder

 
Join Date: Mar 2012
Posts: 62
Thanks: 7
Thanked 0 Times in 0 Posts
cloudstryphe is an unknown quantity at this point
multiple getelementbyid

Hello. I have a page that is displaying facebook news feeds and I have multiple instances of the same id's and classes in the HTML for display. For each single feed from facebook, I would like to be able to click an "open" button and have a hidden div appear under it which displays comments and such. I know how to do this for 1 feed using javascript, but how do I do it for multiple feeds with the same id's?

The HTML looks something like this:
Code:
<div id="facebookfeed">
       Facebook Feed Info
       <a onclick="opencomment()">Open Comments</a>
</div>
<div id="facebookcomments">
    facebookcomments
</div>
The I have the div "facebookcomments" as display: none;. When I have one single facebook feed, I can easily have the comment display and hide, but when I have multiple instances of the above html, the javascript only opens and closes the first comment box. How would I make the javascript work to open only each individual commentbox?

Here's my current basic javascript to open 1 commentdiv.

Code:
function opencomment() {
       var divstyle = new String();
        divstyle = document.getElementById("facebookcomments").style.display;
        if(divstyle.toLowerCase()=="block" || divstyle == "")
        {
            document.getElementById("facebookcomments").style.display = "none";
        }
        else
        {
            document.getElementById("facebookcomments").style.display = "block";
        }
}
Thank you in advance!

Last edited by cloudstryphe; 05-24-2012 at 09:19 PM..
cloudstryphe is offline   Reply With Quote
Old 05-24-2012, 09:31 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
if you have multiple elements sharing the same ID that is illegal html and it will only confuse the javascript. To begin with you should investigate how you can append something to those IDs (usually a number but it doesn't have to be) to make them unique and thus easier to reference
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
cloudstryphe (05-27-2012)
Old 05-24-2012, 09:39 PM   PM User | #3
cloudstryphe
New Coder

 
Join Date: Mar 2012
Posts: 62
Thanks: 7
Thanked 0 Times in 0 Posts
cloudstryphe is an unknown quantity at this point
So you're saying change "facebookcomments" to "facebookcomments 1"? At that point I could just repeat the javascript code for each div, right?
cloudstryphe is offline   Reply With Quote
Old 05-24-2012, 09:42 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
If you want to attach the same value to multiple tags you should use a class rather than an id. You can only have one instance of any single id in a web page but can have as many instances of a class as you like.

Using a class you can then simply reference each tag with that class numerically within the script itself.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
cloudstryphe (05-27-2012)
Old 05-24-2012, 09:46 PM   PM User | #5
cloudstryphe
New Coder

 
Join Date: Mar 2012
Posts: 62
Thanks: 7
Thanked 0 Times in 0 Posts
cloudstryphe is an unknown quantity at this point
Ok, I'll change them to classes. When you say address them numerically, do you mean by adding a "1" at the end of each class in the html then referencing it that way in the javascript?
cloudstryphe is offline   Reply With Quote
Old 05-25-2012, 03:16 AM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by cloudstryphe View Post
Ok, I'll change them to classes. When you say address them numerically, do you mean by adding a "1" at the end of each class in the html then referencing it that way in the javascript?
No.

When you use getElementsByClassName() you get back a list of all the tags with that class. You can then set up a for loop to process through that list.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
cloudstryphe (05-27-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:43 PM.


Advertisement
Log in to turn off these ads.