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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:51:41+00:00 2026-06-04T14:51:41+00:00

I’m trying to select 2 of 3 existing a elements from a repeating HTML

  • 0

I’m trying to select 2 of 3 existing a elements from a repeating HTML structure and having trouble designing an efficient jquery selector.

Here is the markup. I’m trying to select Link 1, Link 2, but not Link 3. Note that the entire HTML structure (div.container) could occur multiple times. If it does, I need Link 1 and Link 2 from all container classes.

NOTE 1: I cannot modify the markup.

NOTE 2: I’d prefer the solution be completely selector based (and avoid calls to .find(), etc., as this selector has to be combined with another selector outside the scope of this question.

<div class="container">
    <h1>
        <a href="http://www.google.ca">Link 1</a>
    </h1>
    <div>
        <div class="left">
            <p>
                <a href="http://www.google.ca">Link 2</a>
            </p>
        </div>
        <div class="right">
            <a href="http://www.google.ca">Link 3</a>
        </div>
    </div>
</div>

When a single container structure occurs, a simple selector is:

$(".container a:lt(2)");

However if multiple container structures exist, the above only selects Link 1 and Link 2 from the first container.

The below works for one or more container structures, but I don’t like it. It seems inefficient to setup multiple css path selectors.

$(".container h1 a, .container .left a");

In verbose language, I would like to “select all a tags within class container not contained within class right. Is this possible? Or is the above selector using 2 CSS paths the best option.

Test Fiddle.

  • 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-04T14:51:43+00:00Added an answer on June 4, 2026 at 2:51 pm

    Try like below,

    $(".container").find("a:lt(2)")
    

    DEMO

    have a separate selector outside the scope of this question, which is then combined with the selector from this question. Let’s say $(‘.myOtherSelector, .selectorFromThisQuestion’); so I’d prefer this solution be a selector only. I can otherwise do $(‘.container’).find(‘a:lt(2)’).add(‘.myOtherSelector’) to combine them but as you can see the first option is much simpler. As I said if it can’t be done, I’ll definitely use your solution/mark as answer. I’m just interested whether a straight selector can be used.

    Alternatively as a workaround you can try like below if you want it in 1 line.. Note that this only will work for the presented markup. Any markup change will break the code.

    $(".container :nth-child(1) a").css('background-color','red');
    

    DEMO

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

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.