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
2010-01-03 17:34:58
bendewey
2010-01-03 17:44:29
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
2010-01-03 17:36:23
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
2010-01-03 17:41:21
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
2010-01-03 17:43:02
http://www.shawnolson.net/a/503/altering-css-class-attributes-with-javascript.html
Jan Hančič
2010-01-03 17:44:01
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
2010-01-03 17:48:06
@alemjerus The link provided by Jan updates the `stylesheet` not the elements and effectively proves your statement incorrect, which it is.
Doug Neiner
2010-01-03 17:52:00
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č
2010-01-03 17:54:34
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
2010-01-03 21:39:37
+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
2010-01-03 17:39:03
I think Matt just wants each box to start with a random colour, rather than cycling through the colours on a timer
adam
2010-01-03 17:41:35
@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
2010-01-03 18:00:09
I think you're right, Doug. I've updated my solution. Thank you :)
Jonathan Sampson
2010-01-03 18:02:18
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
2010-01-03 18:08:10
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
2010-01-03 18:15:57
In that case, does this need to be a purely javascript solution, or can you accept jQuery too?
Jonathan Sampson
2010-01-03 18:42:05
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
2010-01-03 20:04:34
doesn't seem to be working, either i'm implementing it wrong or it's something to do with jQuery and tumblr.
Matt
2010-01-03 20:48:17
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
2010-01-03 17:39:52
@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
2010-01-03 17:57:52
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č
2010-01-03 17:59:35
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
2010-01-03 18:25:04
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č
2010-01-03 17:50:23
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
2010-01-03 18:03:50