Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:09:26+00:00 2026-06-11T11:09:26+00:00

Is it possible, without greatly impacting on the original source code, to extend the

  • 0

Is it possible, without greatly impacting on the original source code, to extend the jQuery .first() method so that it can accept a predicate parameter? Fundamentally what I want it to do is something similar to LINQ’s .First(), C# method. In JavaScript it would look something like this:

function first(arr, predicate) {
    for (var i = 0; i < arr.length; i++) {
        // return the first item fulfilling the predicate
        if (predicate.call(arr[i])) {
            return arr[i];
        }
    }
}

var myArray = [1, 3, 5],
    myPredicate = function() {
        return this >= 2;
    };

var result = first(myArray, myPredicate); // should give '3'

I know it can easily be achieved with JavaScript; I’m just curious if it’s possible to easily extend an existing method within jQuery.

Solution used:

$.fn.firstOne = function(predicate) {
    // validate the predicate is a function
    if ($.isFunction(predicate)) {
        // iterate through each item, finding the first match
        for (var i = 0; i < this.length; i++) {
            if (predicate.call(this.eq(i), i)) {
                return this.eq(i);
            }
        }
    }

    // maintain chainability
    return $();
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:09:28+00:00Added an answer on June 11, 2026 at 11:09 am

    You can easily (and safely) create your own methods as needed,

    $.fn.firstfn = function(fn) {
        return this.filter(fn).first()
    }
    
    $("#nav > li").firstfn(function() {
        return $(this).text() === "John Smith";
    });
    

    However I don’t suggest you override existing methods. Just add your own methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible without using exponentiation to have a set of numbers that when
There's an open source battery indicator that I greatly prefer to the default one.
First, I am very new to lisp, so it is possible that I'm just
This situation can only occur without name mangling (I believe), so the below code
Is it possible—without adding an extra wrapper element—to offset the position of text in
Is is possible (without external library such as boost) to prompt for input from
Is it possible in IE8 without using third party plugin like Java?
Possible Duplicate: Modify the URL without reloading the page I'm looking for a way
Possible Duplicate: JavaScript Variable inside string without concatenation - like PHP In PHP, double
Possible Duplicate: PHP create a file without fopen I want to create a file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.