Web Analytics Made Easy -
StatCounter getting elements by Class... - CodingForum

Announcement

Collapse
No announcement yet.

getting elements by Class...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • getting elements by Class...

    i'm using a simple couple scripts from http://www.theimposter.org/ to show and hide blocks of information when you click on a 'parent' element, so to speak. not in the DOM sense or anything, just a description.

    anyway, before i've confused everybody thoroughly, here's what i've got:

    Code:
    function expand(q) {
    	if (document.getElementById(q).style.display == 'none') {
    		document.getElementById(q).style.display = 'block';
      } else {
    		document.getElementById(q).style.display = 'none';
      }
    }
    the above section shows and hides the elements. that all works great. no problems.

    Code:
    function hideAll() {
    	var Nodes = document.getElementsByTagName('h6')
    	var max = Nodes.length
    	
    	for(var i = 0;i < max;i++) {
    		var nodeObj = Nodes.item(i)
    		nodeObj.style.display = 'none';
    	}
    }
    this is the block of code that is supposed to make sure all the elements you can show/hide are hidden by default. now, the script came to me using that getElementsByTagName method which works great, but isn't very sensical to me. as you can see, i had to use <h6> as it is the only element i could think of that isn't used for some other purpose. i.e. <divs> or <spans> are out because i use them other places on the site and of course don't want them hidden!

    now, i figured it would be easy enough to modify that script to check for all elements of a certain tag name, and then within that for loop check for ones with a certain class attribute defined.

    rather than post what i tried, (partly because i don't remember exactly, and i'm not very skilled with JS), i was wondering if somebody could help me with the solution to this. i know that it's got to be painfully simple.

    thanks.

  • #2
    Have a look at <http://www.web-graphics.com/mtarchive/000832.php>
    liorean <[[email protected]]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

    Comment


    • #3
      thanks. i thought it was kind of funny too, that when looking for information on this topic, i found this page:



      Comment


      • #4
        the Class link on that page times out..

        guess i'll keep trying and hope it's a temporary problem.

        Comment


        • #5
          What can I say - I'm used to answering questions like this

          That code I wrote in it is illegal, though' t[i].class=='blah' should be t[i].className=='blah'.
          liorean <[[email protected]]>
          Articles: RegEx evolt wsabstract , Named Arguments
          Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
          Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

          Comment


          • #6
            mmkay. that might explain why some code i tried didn't work.

            so when referring to an object's class attribute it should always be: object.className?

            Comment


            • #7
              Yes. class is a reserved word in JavaScript, so they needed to use something else for representing it in JavaScript. Same goes for for. Then we have some special cases, such as event handlers and teh style attribute, which do not behave the same when set from the DOM as they do when included in the document.
              liorean <[[email protected]]>
              Articles: RegEx evolt wsabstract , Named Arguments
              Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
              Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

              Comment


              • #8
                just cause I am very curious what exactly do you use class for?

                Comment

                Working...
                X