Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    63
    Thanks
    7
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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

  • Users who have thanked xelawho for this post:

    cloudstryphe (05-27-2012)

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    63
    Thanks
    7
    Thanked 0 Times in 0 Posts
    So you're saying change "facebookcomments" to "facebookcomments 1"? At that point I could just repeat the javascript code for each div, right?

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    cloudstryphe (05-27-2012)

  • #5
    New Coder
    Join Date
    Mar 2012
    Posts
    63
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    cloudstryphe (05-27-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •