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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:05:56+00:00 2026-05-15T23:05:56+00:00

Using jQuery I would like to locate a preceding node from a given node.

  • 0

Using jQuery I would like to locate a preceding node from a given node. The preceding node and the given node may not have the same parent node! Please check the following fragment:

<div class="container">
<div id="sec1">
    <p>Some text</p>
    <p><b>Some</b> text<</p>
    <p>Some <b>more</b> text</p>
</div>
<div id="sec2">
    <p>just a text</p>
</div>
<div id="sec3">
    <p>another <span id="cursorPos1"></span>text</p>
    <p><b>yet</b> another<span id="cursorPos2"></span> text</p>
</div>

Supposing if the current given node is $("span#cursorPos1"), and what I would like to locate is the preceding bold text, then the result should be “<b>more</b>“. Here, the given node is in div#sec3, and the target preceding node is in div#sec1.

If the current given node is $("span#cursorPos2"), then the preceding bold text is “<b>yet</b>“. Here, both the given node and the target preceding node are in the same div#sec3.

Basically, the hierarchy should not be considered at all. From a given node the selector should simply find the preceding match.

Please let me know on how this can be done.

Thanks
Srikanth

  • 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-15T23:05:56+00:00Added an answer on May 15, 2026 at 11:05 pm

    Not a very nice solution, but i think this does the trick. Idea is to find all bold tags and the cursor element, look for position of the cursor in the resulting array, and then return the element before it.

    just threw this together, there are probably better ways. if you use this, some error checking is probably in order too.

    prevBold = function(myId) {
    
    var boldAndMe = $('#' + myId + ', b').toArray();
        var me = $('#' + myId).get(0);
        var myPos = boldAndMe.indexOf( me );
    
        return myPos > 0 ? $( boldAndMe[myPos-1] ) : null;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using JQuery and would like to do the following. I have 2
I am using jQuery and would like to display the images from my PC,
I have a webapplication that heavily uses JQuery. I would like to start using
Using jQuery and backbone.js, I would like to separate the JavaScript from the HTML
Using jQuery, I would like to extract a number from a <span> and use
I would like to have dockable panels using jquery that behave much like the
I have a drop-down menu that I would like to dynamically update using jQuery.
I am new to using jquery and would like to know how to append
I am using jquery and I would like to find the src of the
I am using jQuery autocomplete and would like to limit the showing results only

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.