How do I access multiple divs properties?

Discussion in 'JavaScript' started by Imozeb, Jun 23, 2010.

  1. #1
    I need to access multiple divs at once. I cannot use ids and I was wondering if there was a way in javascript to access something like htmls class?

    Instead of: document.getElementById()

    I need something like document.getElementByClass()

    Is there any way I can do this?

    Thanks.
     
    Imozeb, Jun 23, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    For example you can do like this:

    
    all_divs = document.getElementsByTagName("div");
    
    if(all_divs.length>0){
      var num = 0;
      for(var i=0; i<all_divs.length; i++){
          the_class = all_divs[i].className;
          pattern = /(\s|^)yourclassname(\s|$)/;
    
          if(!pattern.test(the_class)){
              continue;
          }
    
          the_divs[num] = all_divs[i];
          num++;
      }
    }
    
    Code (markup):
    All divs, which class is yourclassname, are in the array the_divs.
     
    s_ruben, Jun 23, 2010 IP
  3. getlandersgetpaid

    getlandersgetpaid Guest

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you can also use the power of jquery selectors. The each() function iterates over each div that matches the selector:

    $('div.yourclassname').each(function(){
    //do some stuff to your div here
    });
    Code (markup):
     
    getlandersgetpaid, Jun 24, 2010 IP
  4. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Nice function! It isn't working for me though:

    
    all_divs = document.getElementsByTagName('div');
    
    				if(all_divs.length>0){
    				  var num = 0;
    				  for(var i=0; i<all_divs.length; i++){
    					  the_class = all_divs[i].className;
    					  pattern = /(\s|^)[COLOR="red"]main[/COLOR](\s|$)/;
    					
    					  if(!pattern.test(the_class)){
    						  continue;
    					  }
    
    					  [COLOR="red"]document.getElementByTagName(all_divs[i]).style.display = '';[/COLOR]
    					  //the_divs[num] = all_divs[i];
    					  
    					  num++;
    				  }
    				}
    
    
    Code (markup):
    It's supposed to show the divs who have 'main' as their class. But it isn't finding them? I checked and it did loop through all the divs but it isn't finding the one I want. Why is this?

    Thanks.
     
    Imozeb, Jun 24, 2010 IP
  5. tdemetri

    tdemetri Peon

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i've worked it out this way - a test example can be seen here:
    demetri-media.com/JavaScript/AllDivtags.html

    clicking the button does a function to change the background of all divs with a class of 'dev' to be red.
    this is the code i wrote to do that. actually , quite simple....
    function change(){
    	all_divs = document.getElementsByTagName("div");
    	for (var i=0;i<all_divs.length;i++){
    	if (document.getElementsByTagName('div')[i].className =='[COLOR="Red"]dev[/COLOR]')
    {document.getElementsByTagName('div')[i].[COLOR="Red"]style.backgroundColor='red'[/COLOR];}
    	}
    }
    Code (markup):
    just change the div calss name, and change the function ( evrything that's in red...) to be what you need.hope that helps!
     
    tdemetri, Jun 24, 2010 IP
  6. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #6
    Imozeb

    try this example

    
    all_divs = document.getElementsByTagName('div');
    
    if(all_divs.length>0){
      for(var i=0; i<all_divs.length; i++){
    	  the_class = all_divs[i].className;
    	  pattern = /(\s|^)main(\s|$)/;
    
    	  if(!pattern.test(the_class)){
    		  continue;
    	  }
    
    	  all_divs[i].style.display = '';
      }
    }
    
    Code (markup):
     
    s_ruben, Jun 24, 2010 IP
  7. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks s_ruben it worked.
     
    Imozeb, Jun 28, 2010 IP