SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    display class using javascript

    hi JavaScript people,

    I have fives div with a class tag when a page loads that is display:none; At some point I want
    the divs to display and to keep it semantically correct I have used classes. But I can't seem
    to get them to display when a link is clicked. Below is where I have got to any help would be great.

    Code:
    	function displayDivs(){
    
    		var displayMyclass = document.getElementsByTagName("myclass");
    		displayMyclass.style.display = "block";
    		
    	}	
    }

  2. #2
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,319
    Mentioned
    462 Post(s)
    Tagged
    8 Thread(s)
    "myclass" isn't a tag name, but a class name. getElementByClassName isn't supported in all browsers, though, so you may need something more elaborate.

  3. #3
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try to display elements using jquery.
    Something like var elements = $(".myclass");
    And then use "elements" variable like an array of your elements, which have a necessary class.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,868
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    JQuery is overkill if all you are after is a class name lookup.

    See http://javascriptexample.net/dom04.php for a working example of how to add getElementsByClassName to those browsers that don't support it while using the built in call in those browsers that do.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

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