0

While reading the jQuery documentation about the jQuery object I came upon this line:

Alternatively, because the jQuery object is "array-like," it supports array subscripting via brackets:

// Selecting only the first 'h1' element on the page (alternate approach)
var firstHeaderElem = $("h1")[ 0 ];

I tested this out using three paragraphs with the jQuery like this:

$(document).ready(function() {
    var t = $("p")[0];
    $("button").click(
    function()
    {
        t.fadeOut();
    });
});

It doesn't work. Is this because using array notation doesn't really return a jQuery object, thus the methods are not available?

3
  • Did you try $(t).fadeOut()? Commented May 7, 2013 at 15:33
  • 1
    It works. @likeitlikeit is right: check this out jsfiddle.net/2SVsr Commented May 7, 2013 at 15:34
  • yeah typo now working Commented May 7, 2013 at 15:35

1 Answer 1

5

When you use the array notation you are getting the native DOM elements back, not the jQuery "wrapped set". Your example would work if you did $(t).fadeOut(); instead. If you wanted to get the first result of a set without shedding the jQuery goodness, you would do $('p').eq(0);

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

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.