2

I believe it's possible to pass an array of DOM objects to jQuery's selector so you can manipulate multiple objects at the same time. I've tried doing this as follows but can't get it to work for some reason...

$(Sel).animate({
        backgroundColor: "#FF0000"
    }, 250, 'linear', function() {

        $(this).animate({
            backgroundColor: "#FFFFFF"
        }, 250, 'linear');

    });

Is it actually possible to do this or am I barking up the wrong tree?

I've put together this jsFiddle to test things out. The aim is to make a booking system where slots of half hour are selected so I need to manipulate "this" and the cell below on the next row.

Any advice greatly appreciated.

Code from fiddle:

function HighlightCells() {

    $('table#Calendar tbody tr td:not(".TimeCell")').live('mouseenter', function() {
        var Sel = new Array();
        Sel[1] = $(this);

        // Count number of previous TDs. Resut is base 0
        var NumIn = $(this).prevAll('td').length;

        // Increment count to compensate for nth-child being base 1
        NumIn++;

        var NextRow = $(this).closest('tr').next('tr');

        Sel[2] = $(NextRow).children("td:nth-child(" + NumIn + ")");

        // Animate the cell background colour red to white
        $(Sel).animate({
            backgroundColor: "#FF0000"
        }, 250, 'linear', function() {

            $(this).animate({
                backgroundColor: "#FFFFFF"
            }, 250, 'linear');

        });


        $('table#Calendar tbody td').live('mouseleave', function() {
            $(this).text("");
        });

    });

}

HighlightCells();
1
  • 1
    According to the documentation, it is possible: "jQuery (elementArray) : An array containing a set of DOM elements to wrap in a jQuery object." What is Sel? edit: nvm, didn't have a look at the fiddle. Commented Aug 13, 2012 at 15:41

3 Answers 3

3

You are making a jQuery object from an array of jQuery objects. You can't do that, it doesn't work.

You need to either make Sel an array of DOM elements (note: arrays are zero-indexed, so Sel[1] is actually the 2nd element, but when building arrays, use .push unless you really need to use the actual keys):

var Sel = [];  // this is preferred over `new Array()`
Sel.push($(this).get(0)); // or Sel.push(this)
// ...
Sel.push($(NextRow).children("td:nth-child(" + NumIn + ")").get(0));

Or make Sel a jQuery object to begin with, and then add elements into it.

var Sel = $();
Sel = Sel.add(this);
// ...
Sel = Sel.add($(NextRow).children("td:nth-child(" + NumIn + ")"));
// ...
Sel.animate({ // sel is already a jQuery object, so we don't need `$(Sel)`
Sign up to request clarification or add additional context in comments.

Comments

2

You are using an array of jQuery objects. Instead, you need an array of DOM objects.

var Sel = new Array();
        Sel[1] = this;

and

Sel[2] = $(NextRow).children("td:nth-child(" + NumIn + ")").get();

though, shouldn't that be Sel[0] = this and Sel[1] = ...?

3 Comments

Don't you mean var Sel = new Array;?
Yes, that's a typo in the fiddle.
@Rocket Don't you mean var Sel = ["why is this sparse", this] :P
1

You can do this

var Sel = new Array();
Sel[1] = this;

and

Sel[2] = NextRow.children("td:nth-child(" + NumIn + ")")[0]; 
//  retrieves the DOM element  
// Also no need to wrap NextRow with $() since it's already a jQuery object

http://jsfiddle.net/wirey00/AX3C8/27/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.