views:

204

answers:

5

I'm in the middle of theming a tumblr site, and each image posted is contained within a ".photobox" class. Is there a piece of javascript or jQuery i can use to change the "border-color" attribute of ".photobox" in the CSS to a random color - preferably chosen from a list of pre-defined colours? I've found one or two scripts on here but they don't seem to work.

A: 

I'm not sure, it may be possible by this http://plugins.jquery.com/node/11985 download zip and see source code of file. It;s for table and colors are random but i think we can give fixed color code

metal-gear-solid
bendewey
A: 

Javascript can not change CSS ruleset. You will have to declare a new CSS ruleset and assign it to DIV or to each image. Like

<script type="text/javascript">
    function changeClass()
    {
        document.getElementById("MyElement").className += " MyClass";
    }
</script>
...
<button onclick="changeClass()">My Button</button>
alemjerus
off course it can
Jan Hančič
No, it can't. What it does, it changes (or overrides) the style of an element, but ruleset itself, being applied to another element, remains the same.
alemjerus
alemjerus is right - it can't take a css specifier and adapt the css within. It can however take each item the specifier applies to and change the applied css
adam
http://www.shawnolson.net/a/503/altering-css-class-attributes-with-javascript.html
Jan Hančič
This is exactly it! Rolling over all the elements in a page to find to which ones the class applied to and modify their properties at the place.
alemjerus
@alemjerus The link provided by Jan updates the `stylesheet` not the elements and effectively proves your statement incorrect, which it is.
Doug Neiner
Besides OP didn't talk about that. He simply wants to change border color of images, you don't need to modify CSS rules here. Just get the images and set a new color.
Jan Hančič
Jan and Doug are right in the end. There are some browsers that support dynamic changes for CSS, including IE, but this is not a common practice.
alemjerus
+5  A: 

Auto-changing Intervals

setInterval(function(){

  // define our colors
  var colors = ["#CCCCCC","#333333","#990099"];
  // get a reference to <div id="mybox"></div>
  var myBox  = document.getElementById("mybox");
  // get a random color from list
  var rand   = Math.floor(Math.random()*colors.length);
  // set random color as borderColor
  myBox.style.borderColor = colors[rand];

}, 500); // run twice a second

One-time Random Color

  // define our colors
  var colors = ["#CCCCCC","#333333","#990099"];
  // get a reference to <div id="mybox"></div>
  var myBox  = document.getElementById("mybox");
  // get a random color from list
  var rand   = Math.floor(Math.random()*colors.length);
  // set random color as borderColor
  myBox.style.borderColor = colors[rand];

Many Images within Single Container

  // define our colors
  var colors = ["#CCCCCC","#333333","#990099"];
  // get all of our images within a specified container element
  var images = document.getElementById("container").getElementsByTagName("img");
  // loop through each
  for (var i = 0; i < images.length; i++) {
    // get a random color from list
    var rand   = Math.floor(Math.random()*colors.length);
    // apply random color to current image
    images[i].style.borderColor = colors[rand];
  }

jQuery Solution

$(".photobox").each(function(){
  var colors = ["#CCCCCC","#333333","#990099"];
  var rand = Math.floor(Math.random()*colors.length);
  $(this).css("borderColor", colors[rand]);
});
Jonathan Sampson
I think Matt just wants each box to start with a random colour, rather than cycling through the colours on a timer
adam
+1 Nice simple solution.
Doug Neiner
@Jonathan, the OP mentions he wants to change the border-color of images. Perhaps a `getElementById('container')` followed by a `getElementsByTagName('img')` and a loop to randomly set each image border to a different color would be appropriate. Dunno, maybe I am reading too much into the question.
Doug Neiner
I think you're right, Doug. I've updated my solution. Thank you :)
Jonathan Sampson
I wish I could +1 again. Love the image loop!
Doug Neiner
Its more of a case that each image is placed inside a ".photobox" class, with the border color of the class defined by "border-color" in the CSS
Matt
So the images don't actually have borders?
Jonathan Sampson
No, sorry, the images don't. I typed my question in a rush and it's only when I saw the flood of answers that I realised I'd worded it wrong.
Matt
In that case, does this need to be a purely javascript solution, or can you accept jQuery too?
Jonathan Sampson
Also, what kind of tag has ".photobox" for its class?
Jonathan Sampson
jQuery would also be fine, as it seems that it works with Tumblr (http://jtheme.tumblr.com/). The .photobox class is placed on the page within a <div> tag.
Matt
If jQuery is fine, see the last solution in my post.
Jonathan Sampson
doesn't seem to be working, either i'm implementing it wrong or it's something to do with jQuery and tumblr.
Matt
Matt, is your page online someplace?
Jonathan Sampson
A: 

There you go: http://jsbin.com/afadu

$(function(){ 
  var colors = ['#ff6','#6ff','#f6f','#f66','#66f','#6f6']; 
  $('input').click(function(){ 
      var randomcolor=Math.floor(Math.random()*colors.length); 
      //alert(randomcolor); 
      $('body').css('color',colors[randomcolor]); 
  }); 
});
Kobi
downvote for using jQuery ...
Jan Hančič
@Kobi, if you update your answer from `body` to `img` and from `color` to `border-color` I will upvote to offset the jQuery downvote. Right now your answer does not exactly address the OP's question (He mentions he needs to change the border-color of images)
Doug Neiner
Just to clarify: I down-voted because OP did not ask for a jQuery solution, but a JavaScript solution. And for not providing a solution to the answer (forgot to write that in the first comment).
Jan Hančič
no problem. You are free to do so. The code that get a random string from the array has nothing to do with jQuery though...
Kobi
A: 

Based on your comment you want something like this:

function getElementsByClassName ( classname, node ) {
    if ( !node ) {
        node = document.getElementsByTagName ( "body" )[0];
    }

    var a = [];
    var re = new RegExp ( '\\b' + classname + '\\b' );
    var els = node.getElementsByTagName ( "*" );
    for ( var i = 0, j = els.length; i < j; i++ ) {
        if ( re.test ( els[i].className ) ) {
            a.push ( els[i] );
        }
    }

    return a;
}

var photoboxes = getElementsByClassName ( 'photobox' );
var colors = Array ( 'red', 'blue', 'green', 'pink' );
for ( var i = 0; i < photoboxes.length; i++ ) {
    var images = photoboxes[i].getElementsByTagName ( 'img' );

    for ( var j = 0; j < images.length; j++ ) {
        images[j].style.borderColor = colors[Math.floor ( Math.random () * colors.length )];
    }
}

The getElementByClassName was copied from here.

Jan Hančič
yeah - that's exactly it - every image has a randomly selected color. although each image is held within a "photobox" in the main body <html>.photobox{ overflow:hidden; width:400px; max-height:300px; border: solid 5px; border-color: #ffffff; }</html>
Matt