Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 1st, 2004, 08:04 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default Hiding elements using identification by class

In javascript it is possible to show/hide some elements on a page using the display property of style sheets using a function like this...
Code:
function expandSection(sectionId)
{
    var field;
    field = document.getElementById(sectionId);
    if(field != null)
        field.style.display = (field.style.display != "block")? "block" : "none";
}
However I would like to address a class of elements, and not only one element. I have got a table in which I would like to show some rows in a simple mode and in an advanced mode I would like to show all rows.

How is this possible!? Is there e.g. a function getElementsByClass, which return an array of elements!?

Thanks

Jacob.
__________________
Danish audio books for download at http://www.lytenbog.dk (Danske lydbøger til download).
Reply With Quote
  #2 (permalink)  
Old February 1st, 2004, 08:26 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If you mean for elements with a class="highlight" or similar you wish to get all the elements by their class name then no, there's no such method.
You would have to loopthrough all ther elements in the body or a known subsection and test their className. You would need to be careful as elements can have more than one class, class="highlight red" for instance. If your target browser was IE5.5 or above you could use a regular expression:
Code:
function hasClass(Element, ClassName)
{
  var oRE = new RegExp("\\b" + ClassName + "\\b", "i");
  return oRE.test(Element.className);  
}


--

Joe
Reply With Quote
  #3 (permalink)  
Old February 1st, 2004, 08:48 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply, then I think I will try to make two functions toggleSection and toggleSections, where the last have some argument specifying the pattern and call the toggleSection function!

Now another problem emerged! I try this...
Code:
<INPUT CLASS='button' type='button' onClick="alert('something'); simple(); return false;" value='advanced'>
...where the simple function is a function that merely have an alert call!

Only the first alert is called, and no errors has occured. What is wrong with this!!? It seems so simple, but it do not work!

Thanks!

Jacob.

Jacob.
Reply With Quote
  #4 (permalink)  
Old February 2nd, 2004, 06:12 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Seems very strange, can you show the actual function and also the definition of the class 'button'?



--

Joe
Reply With Quote
  #5 (permalink)  
Old February 2nd, 2004, 03:52 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry! Finally I found the error. Why is it that it is always the small errors that is hard to find...
Code:
for(i = start; i < ender, i++)
The error is in this one! Pretty obvious when extracted from the context.

I do not know why IE did not show that yellow icon at the bottom of the page!

Thanks Joe :)

Jacob.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
About Anonymous Identification Lee Dumond BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 15 December 14th, 2008 05:12 PM
Dropdownlist Items Identification msbsam ASP.NET 2.0 Professional 1 August 18th, 2008 12:50 PM
Hiding Publicly Declared Base-Class Methods icecrew VB.NET 1 November 17th, 2006 05:42 PM
Regarding uniquely identification of every struts vishal_java2 Struts 0 April 11th, 2006 02:45 AM
Regarding uniquely identification of every struts vishal_java2 Struts 0 April 11th, 2006 02:42 AM



All times are GMT -4. The time now is 06:21 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.