[solved] Same Color to multiple divs in one class

here you will find only solved Help topics

Moderator: 1.2 Moderators

[solved] Same Color to multiple divs in one class

Postby exgaudt on Thu Feb 04, 2010 11:26 pm

Hello everybody! I Have really confusing problem... I want to change color simultaneously in multiple different div blocks and i want them to have one color!

Here is my not working code:

  1.  
  2. <html>
  3. <head>
  4. </head>
  5. <body>
  6. <script type="text/javascript" src="mootools2.js"></script>
  7. <script type="text/javascript">
  8.    
  9.        window.addEvent('domready', function(){
  10.  
  11.   function rgb() {
  12.  
  13.     var r = $random(0, 220);
  14.     var g = $random(0, 220);
  15.        var b = $random(0, 220);
  16.  
  17.     var rgb = [r,g,b].rgbToHex();
  18.  
  19.        return rgb;
  20.   }
  21.  
  22.  
  23.    $("#colarray .barcode").addEvents({
  24.     'mouseover': function(){
  25.  
  26.          this.morph({
  27.                  'background-color': rgb()
  28.          });
  29.     },
  30.     'mouseleave': function(){
  31.          this.morph({
  32.                  'background-color': rgb()
  33.          });
  34.     }
  35.   });
  36.      });
  37. </script>
  38.  
  39. <style>
  40. #colarray {
  41. width: 200px;
  42. height: 200px;
  43. border: 1px;
  44. }
  45. .barcode {
  46. background-color: #251151;
  47. }
  48.  
  49.  
  50. </style>
  51.  
  52. <div id="colarray">
  53. test
  54. <div class="barcode">test2</div><br>
  55. <div class="barcode">test222</div><br>
  56. <div class="barcode">test2222</div><br>
  57.  
  58. </div>
  59. </body>
  60. </html>
  61.  
  62.  


But if i use this $('barcode') instead of $("#colarray .barcode")
And one div block will have id="barcode" (this id will be determined in style) So, this block wil change the color... So is there any possibility to make it work as i want?
Last edited by exgaudt on Fri Feb 05, 2010 11:27 pm, edited 1 time in total.
exgaudt
 
Posts: 2
Joined: Thu Feb 04, 2010 11:13 pm

Re: Same Color to multiple divs in one class

Postby fiote on Fri Feb 05, 2010 2:42 am

First, you're using one dollar. When you use $, mootools search for ONE element with the ID passed.

Fix it adding one more dollar =). Now it will search for any elements within the parameters (using ids and classes, just like you did).

  1. $("#colarray .barcode") // one dollar, wrong
  2. $$("#colarray .barcode") // two dollars, right


Ok, now its working.

But, by your post i GUESS that you want all them to morph to the same color when you move-over one of them. If its right, try this:

  1. window.addEvent('domready', function(){
  2.  
  3.     function rgb() { // your function
  4.         var r = $random(0, 220);
  5.         var g = $random(0, 220);
  6.         var b = $random(0, 220);
  7.         var rgb = [r,g,b].rgbToHex();
  8.         return rgb;
  9.     }
  10.  
  11.     var allBars = $$("#colarray .barcode"); // getting all barcodes
  12.    
  13.     allBars.addEvents({ // adding the events
  14.         'mouseover' : function(){ allBars.morph({'background-color': rgb()}); }, // morphin ALL them
  15.         'mouseleave': function(){ allBars.morph({'background-color': rgb()}); }  // to the new color
  16.     });
  17.    
  18. });
$5 bucks for a little code almost-ready to use! Send me a PM =]
(its for a good cause man! i need to pay my wow account ^^)
User avatar
fiote
mootools fan
 
Posts: 50
Joined: Sun Nov 29, 2009 3:01 pm
Location: Brazil

Re: Same Color to multiple divs in one class

Postby exgaudt on Fri Feb 05, 2010 11:26 pm

Thank you very much! Your solution worked perfectly for me! Last thing left - to correct and to make fixed ranges of color :)
exgaudt
 
Posts: 2
Joined: Thu Feb 04, 2010 11:13 pm


Return to Solutions

Who is online

Users browsing this forum: Google [Bot] and 0 guests