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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:08:42+00:00 2026-06-14T15:08:42+00:00

I am having issues with getting exactly values with Javascript. Following is working version

  • 0

I am having issues with getting exactly values with Javascript.

Following is working version of when we have class on single item.
http://jsfiddle.net/rtnNd/

Actually when code block has more items with same class (see this: http://jsfiddle.net/rtnNd/3), it picks up only the first item which use the class name.

My issue is that I would like to pick up only the last item which use the class name. So I used following code:

var item = $('.itemAnchor')[6]; 
var href = $($('.hidden_elem')[1].innerHTML.replace('<!--', '').replace('-->', '')).find(item).attr('href'); 

But it doesn’t work though. I don’t really know why.

The code that may contains items are using same class, class may be use in 2 items, 3 items, or 6th times. That’s why, I want to pick up only the last item to extract.

Can you explain, thank you all for reading my question.

  • 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-14T15:08:43+00:00Added an answer on June 14, 2026 at 3:08 pm

    “My issue is that I would like to pick up only the last item which use the class name.”

    OK, so in a general sense you would use the .last() method:

    var lastItem = $('.itemAnchor').last();
    

    Except that there are no elements in the DOM with that class because (in your fiddles) they’re all commented out. This is also the reason why the code you showed in your question didn’t work. The first line:

    var item = $('.itemAnchor')[6];
    

    …sets the item variable to undefined. The selector '.itemAnchor' returns no elements, so $('.itemAnchor') is an empty jQuery object and it has no element at index 6.

    You need to use the '.itemAnchor' selector on the html that you get after removing the opening and closing comments with your .replace() statements, so:

    var href = $($('.hidden_elem')[0].innerHTML.replace('<!--','').replace('-->',''))
              .find('.itemAnchor').last().attr('href');
    

    Demo: http://jsfiddle.net/rtnNd/4/

    EDIT in response to comment:

    “How can we pick up the itemElement before that last one.”

    If you know you always want the second-last item use .slice(-2,-1) instead of .last(), as shown here: http://jsfiddle.net/rtnNd/5/

    Or if you know you want whichever one has an href that contains a parameter h= then you can use a selector like '.itemAnchor[href*="h="]' with .find(), in which case you don’t need .last() or .slice():

    var href = $($('.hidden_elem')[0].innerHTML.replace('<!--','').replace('-->',''))
              .find('.itemAnchor[href*="h="]').attr('href');
    

    Demo: http://jsfiddle.net/rtnNd/6/

    Note though that this last method using the attribute-contains selector is picking up elements where the href has the text “h=” anywhere, so it works for your case but would also pick up hh=something or math=easy or whatever. You could avoid this and test for just h= as follows:

    var href = $($('.hidden_elem')[0].innerHTML.replace('<!--','').replace('-->',''))
        .find('.itemAnchor')
        .filter(function() {
            return /(\?|&)h=/.test(this.href);
        }).attr('href');
    

    Demo: http://jsfiddle.net/rtnNd/7/

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

Sidebar

Related Questions

I am having issues getting my remove element function working. I have this function:
I'm having issues getting around websites that use http authentication, I have a list
Im having issues getting this to work, maybe its not even possible? I have
I'm having issues getting the CSS3 box-shadow to look correct. I currently have a
I'm having some issues getting file uploads to work correctly and the following code
So I'm having pretty much exactly the problem described here: http://code.google.com/p/android/issues/detail?id=6191 and until the
I am having issues getting the network.request function in Corona SDK working with a
I having issues getting Django to work with settings.py at different location I have
Having issues getting django custom commands to work. From django documetation , have placed
I'm having issues getting Firefox to update a webpage when its class is changed

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.