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 6547233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:48:17+00:00 2026-05-25T11:48:17+00:00

Sorry for this very basic JQuery question. I created ‘mytest’ jquery function like this:

  • 0

Sorry for this very basic JQuery question. I created ‘mytest’ jquery function like this:

jQuery.fn.mytest = function () {
    alert($(this).attr('id'))
}

Now if I call it like this everything is perfect:

$("#someid").mytest();

It alerts “someid”. But if I do something like this:

$("#someid, #anotherid, #moreids").mytest();

this function only alerts “someid”. Of course “anotherid” and “moreids” exist.

Why mytest() is not working and what is the right code for this function?

  • 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-05-25T11:48:18+00:00Added an answer on May 25, 2026 at 11:48 am

    Your code is adding a “plugin” to jQuery that makes your function available on jQuery instances. The reason you’re only seeing one ID in your function is that you’re using attr, which only retrieves the first element’s attribute. (Also, you really don’t need attr to get an id value.)

    Your function should look something like this (live copy):

    jQuery.fn.mytest = function () {
        var ids = jQuery.map(this, function(elm) {
            return elm.id;
        });
        alert(ids.join(","));
    };
    

    …to show the id values (if any) of each element in the currently-matched set.

    Or you could do it with a simple loop rather than with jQuery.map (live copy):

    jQuery.fn.mytest = function () {
        var ids, index, elm;
    
        ids = [];
        for (index = 0; index < this.length; ++index) {
            elm = this[index];
            if (elm.id) {
                ids.push(elm.id);
            }
        }
        alert(ids.join(","));
    };
    

    Also, note that within a jQuery plug-in function, this is the current jQuery instance, so you don’t need (or want) to pass it through $() to create a jQuery wrapper around the element (you do that in event handlers, not plug-ins). So when I do this.length in the second example above, I’m using the length property of the jQuery instance that we’re currently operating on. When I index into this using bracketed notation (elm = this[index];), I’m indexing into the jQuery instance’s matched set of elements (like the get method, but more directly).

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

Sidebar

Related Questions

Sorry if this is very basic as a question....but I can't my head around
Sorry for the very basic question. What does the & operator do in this
Sorry for this, may be this is very basic question, I have multiple small
Sorry for english this might be very basic Question, I have an Application where
I'm sorry for this very newbish question, I'm not much given into web development.
Sorry this is not a very well defined question, I am thinking about an
Sorry if this question is very easy, but I can't find the answer anywhere.
Sorry, I know this is a very lame question to ask and not of
This is a question corresponds to my uni assignment so I am very sorry
Ok sorry this might seem like a dumb question but I cannot figure this

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.