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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:54:15+00:00 2026-05-14T01:54:15+00:00

(I’ve removed attributes, but it’s a bit of auto-generated HTML.) <img class=p/> <div> hello

  • 0

(I’ve removed attributes, but it’s a bit of auto-generated HTML.)

<img class="p"/>
<div> hello world
    <p>
        <font><font size="2">text.<img class="p"/>
        some text
        </font></font>
    </p>
    <img class="p"/>
    <p> <font><font size="2">more text<img class="p"/>
        another piece of text
        </font></font>
    </p><img class="p"/> some text on the end
</div>

I need to apply some highlighting with backgrounds to all text that is between two closest (in the HTML code) img.p elements when hovering first of them. I have no idea how to do that. Lets say I hover the first img.p – it should highlight hello world and text. and nothing else.

And now the worst part – I need the backgrounds to disappear on mouseleave.

I need it to work with any HTML mess possible. The above is just an example and structure of the documents will differ.

Tip: Processing the whole html before binding hover and putting some spans etc. is OK as long as it doesn’t change the looks of the output document.

  • 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-14T01:54:15+00:00Added an answer on May 14, 2026 at 1:54 am

    Processing the whole html before binding hover and putting some spans etc. is ok

    You certainly would have to do that, as you can’t style text nodes, only elements.

    Here’s a function you could use to do it from script. (Unfortunately jQuery isn’t much use here as it doesn’t like handling text nodes.)

    // Wrap Text nodes in a new element of given tagname, when their
    // parents contain a mixture of text and element content. Ignore
    // whitespace nodes.
    //
    function wrapMixedContentText(el, tag) {
        var elementcontent= false;
        for (var i= el.childNodes.length; i-->0;) {
            var child= el.childNodes[i];
            if (child.nodeType===1) {
                elementcontent= true;
                wrapMixedContentText(child, tag);
            }
        }
        if (elementcontent) {
            for (var i= el.childNodes.length; i-->0;) {
                var child= el.childNodes[i];
                if (child.nodeType===3 && !child.data.match('^\\s*$')) {
                    var wrap= document.createElement(tag);
                    el.replaceChild(wrap, child);
                    wrap.appendChild(child);
                }
            }
        }
    }
    

    And here’s some functions that you could use to select nodes between other nodes. (Again, jQuery doesn’t currently have a function for this.)

    // Get array of outermost elements that are, in document order,
    // between the two argument nodes (exclusively).
    //
    function getElementsBetweenTree(start, end) {
        var ancestor= getCommonAncestor(start, end);
    
        var before= [];
        while (start.parentNode!==ancestor) {
            var el= start;
            while (el.nextSibling)
                before.push(el= el.nextSibling);
            start= start.parentNode;
        }
    
        var after= [];
        while (end.parentNode!==ancestor) {
            var el= end;
            while (el.previousSibling)
                after.push(el= el.previousSibling);
            end= end.parentNode;
        }
        after.reverse();
    
        while ((start= start.nextSibling)!==end)
            before.push(start);
        return before.concat(after);
    }
    
    // Get the innermost element that is an ancestor of two nodes.
    //
    function getCommonAncestor(a, b) {
        var parents= $(a).parents().andSelf();
        while (b) {
            var ix= parents.index(b);
            if (ix!==-1)
                return b;
            b= b.parentNode;
        }
        return null;
    }
    

    Possible usage:

    var outer= document.getElementById('myhighlightingimagesdiv');
    wrapMixedContentText(outer, 'span');
    
    var ps= $('#myhighlightingimagesdiv .p');
    ps.each(function(pi) {
        // Go up to the next image in the list, or for the last image, up
        // to the end of the outer wrapper div. (There must be a node
        // after the div for this to work.)
        //
        var end= pi===ps.length-1? outer.nextSibling : ps[pi+1];
    
        var tweens= $(getElementsBetweenTree(this, end));
        $(this).hover(function() {
            tweens.addClass('highlight');
        }, function() {
            tweens.removeClass('highlight');
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.