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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:11:29+00:00 2026-06-08T14:11:29+00:00

Okay, so I have discovered how to write jQuery plug-ins and I think this

  • 0

Okay, so I have discovered how to write jQuery plug-ins and I think this will tidy up my code a lot. I’ve been looking at a lot of tutorials which just adds style to the set of matched elements passed on the plug-in function.

…but I want to add new elements to the when using my function. Say that I want to do this:

$children
    .addCousins()
        .addClass("x")
    .end()
    .addClass("y");

So I’ve written this function, .addCousins(). What it does, nobody knows, but know that it works in theory. I only need to wrap my head around how to add more elements (if there are any) when .addCousins() is called. For each element in $children there can be zero or more “cousins”, which should be added when doing the .each() in the plug-in function.

Here’s what I have so far:

(function($){
    $.fn.extend({
        addCousins: function () {
            return this.each(
                    function () {
                        var index = $(this)
                            .prevAll("td.radio")
                                .andSelf()
                                    .length - 1;

                        $(this)
                            .prev("td[rowspan=2]")
                                .parent()
                                    .next()
                                        .children("td.radio")
                                            .eq(index); // here is the "cousin" which should be added to the selection
                    }
                );
        }
    });
})(jQuery);

I’ve been trying various solutions all day, but no progress yet. The selection of the cousin (the implementation of the selection you need not worry about) is done in the inner anonymous function, but, yeah, I don’t really know how to go from here.

I hope I’ve been more or less clear about what I want.

Edit #1. Here is some testable HTML (I think)…

<table>
    <tr>
        <td rowspan="2">Here are cousins</td>
        <td class="radio">1</td>
        <td class="radio">2</td>
        <td class="radio">3</td>
    </tr>
    <tr>
        <td class="radio">4</td>
        <td class="radio">5</td>
        <td class="radio">6</td>
    </tr>
    <tr>
        <td rowspan="2">Here are NO cousins</td>
        <td class="radio">7</td>
        <td class="radio">8</td>
        <td class="radio">9</td>
    </tr>
</table>

…and my updated plug-in function.

(function($){
    $.fn.extend({ 
        cousins: function () {
            this
                .each(
                    function () {
                        var index = $(this)
                            .prevAll("td.radio")
                                .andSelf()
                                    .length - 1;

                        $(this)
                            .prev("td[rowspan=2]")
                                .parent()
                                    .next()
                                        .children("td.radio")
                                            .eq(index)
                                                .addClass("cousin");
                                            .end()
                                        .end()
                                    .end()
                                .end()
                            .end()
                            .prev()
                                .find("td[rowspan=2]")
                                    .nextAll("td.radio")
                                        .eq(index)
                                            .addClass("cousin");
                    }
                );

            return $("td.cousin")
                .removeClass("cousin");
        }
    });
})(jQuery);

If table cell 2 is selected an passed on to .cousins(), table cell 4 should be returned. Vice versa for table cell 4…

Edit #2. My (not yet working) update:

cousins: function () {
    var $cousins = $();

    this
        .each(
            function () {
                var index =
                    $(this)
                        .prevAll("td.radio")
                            .andSelf()
                                .length - 1;
                var $next = null;
                var $prev = null;

                $next = 
                    $(this)
                        .prev("td[rowspan=2]")
                            .parent()
                                .next()
                                    .children("td.radio")
                                        .get(index);
                $prev =
                    $(this)
                        .parent()
                            .prev()
                                .find("td[rowspan=2]")
                                    .nextAll("td.radio")
                                        .get(index);

                if ($next == null && $prev == null) {
                    $cousins.push($(this));
                } else {
                    $cousins.push($next);
                    $cousins.push($prev);
                }
            }
        );

    return $cousins;
}
  • 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-08T14:11:31+00:00Added an answer on June 8, 2026 at 2:11 pm

    Viktor, assuming your code to correctly select the elements of interest (cousins), then there’s a number of ways to approach adding them to the original selection.

    A consideration is that the elements in the returned jq object should be in DOM order and fortunately, “as of jQuery 1.4 the results from .add() will always be returned in document order (rather than a simple concatenation) .add()“. pimvdb’s code correctly addresses this.

    On another tack, I would prefer the additional flexibility offerd by a .cousins() plugin rather than .addCousins(), where :

    • .cousins() would select just the cousins
    • .cousins().andSelf() would be equivalent to .addCousins()

    Here’s the code for .cousins() :

    (function($) {
        $.fn.extend({
            cousins: function(options) {
                var settings = {
                    sibling_selector: '*',
                    uncle_selector: '*',
                    cousin_selector: '*'
                };
                if (options) {
                    $.extend(settings, options);
                }
                var c = [];//cousins
                this.each(function() {
                    var $this = $(this);
                    var index = $this.parent().children(settings.sibling_selector).index($this);
                    $this.parent().siblings(settings.uncle_selector).each(function() {
                        c.push($(this).children(settings.cousin_selector).get(index));
                    });
                });
                return this.pushStack(c);
            }
        });
    })(jQuery);
    

    DEMO

    Notes:

    • to generalize the plugin, this version accepts an options map (see demo).
    • .pushStack() allows the method chain .cousins(...).andSelf() and .cousins(...).end() to behave as expected.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

okay i have been trying to understand this for hours i am learning VB
Okay so i have this code: if (isset($_GET['book'])) { $query= SELECT book_id, title, authors.`author`
Okay I have been trying to get this to work for some time now.
Okay I have updated my code quite a bit. I am getting a new
Okay I have updated my code a little, but I am still not exactly
Okay i have two models: posts and comments. as you can think comments has
Okay i have already been through most of the ati and nvidia guides to
okay have a this list: object[] test; test[0]=null; .... test[8700]=null; test[8701]= object[] .... test[9431]=
Okay I have a project in CMake structured like this: CMakeLists.txt /libfoo/CMakeLists.txt /frontend/qt/CMakeLists.txt libfoo
Okay I have been working on a website but I keep on having an

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.