0

I'd like to have a function which would return one or more variables connected to element, for example return color of specified border or borders (would use jQuery to it):

$('element').borderColor(); // should return "red, blue, green, orange"
$('element').borderColor('left'); // should return "red"
$('element').borderColor('left', 'right'); // should return "red, green"

and so on.

Here's example: http://jsfiddle.net/DkGpP/

Question 1: How should I construct part of script (which is actually if/else) to return only what I want (actually $('element').borderColor('left', 'right'); still showing only "red")?

Question 2: Can arguments in my function can be called without apostrophes like:

$('element').borderColor(left);
$('element').borderColor(left, right);

if yes, how to rebuild it (actually show "undefined"). Does it demand to build a function like:

jQuery.fn.borderColor = function(left, top, right, bottom) {

?

I'll be grateful for help.

4
  • What happens if $('element') selects multiple elements? should it target only the first found? Commented Apr 15, 2013 at 14:36
  • The answer to your second question is that without apostrophes, left and right are treated as variables, and their values are used as the parameters. If you haven't assigned them, the values are undefined. Commented Apr 15, 2013 at 14:42
  • I would use it to elements which can't be multiple (I mean call them by ID). Commented Apr 15, 2013 at 14:45
  • How could I assign them in this case? Commented Apr 15, 2013 at 15:07

3 Answers 3

1

You don't need an if-else structure.

jQuery.fn.borderColor = function() {    
    var tag = $(this).eq(0), ret = [];
    var arr = arguments.length > 0 ? arguments : ["left","top","right","bottom"];
    for (var i = 0; i < arr.length; i++) {
        ret.push(tag.css("border-" + arr[i] + "-color"));
    }
    return ret.join(","); // remove .join(",") to get a useful array
};

var myborder = $('#tester').borderColor('left', 'right');
$('#result').text(myborder);

Demo

Sign up to request clarification or add additional context in comments.

6 Comments

What if I'd like to add more arguments like: width, height, positions etc.?
Then it would be out of the scope of the plugin, since it's called borderColor. It wouldn't make sense to do that for width and height, just use .css, .width, or .height for it. As far as positions, that also already exists with .position.
I know but there's no position right and bottom builded in jQuery and also I could like to call width or height specified in some way like my "width" would be real width x 2 etc.
Here's the thing. You can make a plugin do whatever you want it to do. At this point it is unclear what you want and you appear to be just trying to get me to do your work for you.
If I'd like you to make work for me I would specified all I need and wait for finished plugin. I asked a question to get a start point and could work on it by myself.
|
1

I would use something like this.

function borderColor(object){
    for(var ele in object){
       if(ele=="left"){
       }else if()....
    }
}

object will be array.

Comments

0

When I have to deal with multiple parameters, I usually prefer to go the "options" way, meaning that the argument is a js object with various property defined.

In your example, I would write the test like this:

if (border == null) // or undefined, add a test here
    return [borderLeft, borderTop, borderRight, borderBottom];

// skip the else
result = [];
if (border.left)
    result.push(borderLeft);
if (border.right)
    result.push(borderRight);
// etc. etc.
return result;

And call the function like this:

var myborder = $('#tester').borderColor({ left: true, right: true });

As you can see I am passing an object, having defined only the left and right properties. I set them to true, but anything that evaluates to a "truthy" value will do.

The fiddle: http://jsfiddle.net/DkGpP/2/

1 Comment

I tired to avoid "options" way but probably it's the most easy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.