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

  • Home
  • SEARCH
  • 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 7896955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:01:39+00:00 2026-06-03T08:01:39+00:00

Assuming I have a JQuery object stored in $e obtained through this selector: var

  • 0

Assuming I have a JQuery object stored in $e obtained through this selector:

var $e = $("input[type='text'][value='" + v + "']");

v represents the $(this).val() which I obtain inside an .each() iteration but it doesn’t actually matter, it can be any numeric value.

I want to create a JQuery set of elements containing $e, the DOM element which comes after it, and the 6 DOM elements previous to $e.

The function itself works perfectly fine, I’m using this code for each iteration:

var $e = $("input[type='text'][value='" + v + "']");
$e = $e.add($e.next());
for (i=0;i<6;i++)
    $e = $e.add($e.prev());
$e.remove();

I guess it’s a little bit hard to explain, so take a look at this JSFiddle.

I’ve wrapped the code above inside an anonymous function bound to the the Remove line 2 button which will pass a fixed v value to my script.

I’m a starter with JQuery and even after taking a good read at JQuery Selectors page I couldn’t find a simpler way to do this. I thought that the .siblings(), or more specifically JQuery(‘prev ~ siblings’) could do it in a simpler way if I could specify the range of the jquery objects array but I couldn’t figure out how.

The HTML is generated through a PHP template which I’d rather avoid editing, so I’ll accept answers which do not include wrapping the given elements inside a container/wrapper.

Is there any simpler way to select the 6 previous elements of a given JQuery selector without wrapping them inside any container or wrapper?

  • 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-03T08:01:41+00:00Added an answer on June 3, 2026 at 8:01 am

    Try using .prevAll and slice the elements you need. See below,

    DEMO

        var $prevAll = $e.prevAll();
        $e = $e.add($e.next()).add($prevAll.slice(0, 6));
    

    Edit: Added $e.add($e.next()) to add next element.

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

Sidebar

Related Questions

Assuming I have a selector for a text node in jQuery, how can I
I have a jQuery which is like this: <script type=text/javascript> $(document).ready(function () { $(#txtAgentName).click(function
Let's say that I have a object that looks like this var object =
Assuming I have the following two JQuery functions - The first, which works: $(#myLink_931).click(function
Assuming I have three tables : TableA (key, value) TableB (key, value) TableC (key,
Assuming I have a tree structure UL --LI ---INPUT (checkbox) And I want to
I have a group of text based rules that are structured like this: Rule
I have strings of numbers... **[0.7955635, 485, .421, 1.08]** Assuming i have a jquery
I have a number of Jquery plugins being called through my Drupal Acquia Marina
Assuming a query as such. var result = $(.ui-th-column); This query in my case

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.