0

I am using jquery 1.11.1.

Once I selected a collection using a selector, i.e var objs = $('.selector_class') how can I get jquery element from that element. I tried objs[0] and objs.get(0). Both give javascript object. is there a way to access one jquery object directly.

0

3 Answers 3

2

use .eq(index)

$('.selector_class').eq(0)

doc

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

Comments

0

With objs[0] and objs.get(0) you're getting the DOM element, not the jQuery object.

You can convert the DOM object to a jQuery element by wrapping it in $(), or get the jQuery elements directly:

<div class="selector_class">Text1</div>
<div class="selector_class">Text2</div>
<div class="selector_class">Text3</div>

<script>
    $(function () {
        var collection = $(".selector_class");
        var a = collection.eq(0).text();
        var b = collection.eq(1).text();
        var c = collection.eq(2).text();

        console.log("a: " + a + " => b: " + b + " => c: " + c);
        // shows: a: Text1 => b: Text2 => c: Text3
    });
</script>

Comments

0

This can be done using .eq( index )

index

Above is of Type: Integer

An integer indicating the 0-based position of the element.

.eq() method constructs a new jQuery object from one element within that set.

When it is provided with an integer, it gives the position of this element in the set.

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.