function bad_color(id,id2) { document.getElementById(id).bgColor = "pink"; document.getElementById(id2).bgColor = "pink"; } The above works as I want, but is there a better way to do this? I'm going to have to pass a lot more than 2 arguments to the function at a time. Pass an array and have a for statement in the function? Thinking out loud. Thanks for any help.
the best practice these days is to pass a single object instead of stacking up multiple arguments and if - then - else and copy/pasting code. for instance, consider using something like this function which can support ANY number of elements passed to it w/o changing the function itself: var setBackgrounds = function(options) { // deal with default options passed (or not passed) options = options || {}; // options should always be an object options.elements = options.elements || ["edit13110491","td_post_13109496"]; // default elements to apply to, array of ids options.backgroundColor = options.backgroundColor || "pink"; // default colour to apply // loop through elements var count = options.elements.length, el; while(count--) { el = document.getElementById(options.elements[count]); if (el) el.style.backgroundColor = options.backgroundColor; } }; setBackgrounds({ elements: ["vB_Editor_001_smiliebox"], backgroundColor: "blue" }); // run with default elements and colour: setBackgrounds(); // run with default elements and a black bg: setBackgrounds({ backgroundColor: "#000" }); Code (javascript): this allows you great flexibility as the options are 'private' to the function and when you need to add new arguments, you don't need to change all places where you call it, just take care of a decent default set and you're off.
I appreciate your response. I am a nube at js, so I have no idea what your response means. I appreciate all the comments, but still no clue.
You could do it like this: // This is creating an array of objects, in format of property: value, property: value... var a = [ {"id":"element1", "color": "red"}, {"id":"element2", "color": "blue"}, {"id":"element3", "color": "green"} ]; bad_color(a); //sends our object to the function function bad_color(args) { for(i=0; i<args.length; i++) { //iterate through each object in the array document.getElementById(args[i].id).bgColor = args[i].color; } } Code (markup):