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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:09:13+00:00 2026-06-15T19:09:13+00:00

I just wrote a really simple jQuery plugin.. sort of, that I want some

  • 0

I just wrote a really simple jQuery plugin.. sort of, that I want some help with.

(function($){
    $.fn.highlight = function(words){
        return this.each(function(){
            //Get text from within
            var text = $(this).html();
            //Replace with new text
            $(this).html(text.replace(words,"<i class='highlight'></i><span class='word'>"+"$1"+"</span>"));
            //Get the all the highlight classes within this
            var highlights = $(this).find(".highlight");
            //Go through all
            return highlights.each(function(){
                var $this = $(this);
                //Get to the next word
                var wordDiv = $this.nextAll(".word").eq(0);
                //Set highlight span same height as word
                $this.height(wordDiv.height()+2);
                //Set highlight span same width +4 then positioning
                var newWidth = wordDiv.width()+4;
                wordDiv.replaceWith(function(){
                   return $(this).contents();
                });
                $this.width(newWidth+2).css({
                    left:(newWidth)+"px",
                    marginLeft: "-"+(newWidth)+"px"
                });
                $this.delay(Math.ceil(Math.random()*30)*200+2000).fadeOut("4000",function(){
                  $this.remove();
             });
        });         
        });
    }
})(jQuery);
$(document).ready(function(){
    $("div").highlight(/(simple|wrote|knowledge)/g);
});​

and the CSS:

.highlight{
    background: #FBB829;
    display: inline-block;
    position: relative;
    z-index: -1;
    top: 5px;
    -moz-border-radius: 4px;
    border-radius: 4px;
}​

and is it better a better practice to put that CSS in the jQuery plugin?
Here’s a jsFiddle:

http://jsfiddle.net/aVCtA/11/

and you see my text moves when the last .highlight span disappears. Why’s that? Thought relative and z-index: -1 would fix that?

Should I instead use position absolute and calculate the positioning?

  • 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-15T19:09:15+00:00Added an answer on June 15, 2026 at 7:09 pm

    Made some changes to your code, check the test on jsfiddle

    Changelog:

    jQuery:

    Removed the .css() from the following line,

    $this.width(newWidth + 2);
    

    CSS:

    Changed the styling to,

    .highlight{
        background: #FBB829;
        display: inline-block;
        position: absolute;
        z-index: -1;
        top: 0px;
        margin-left: -2px;
        -moz-border-radius: 4px;
        border-radius: 4px;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this really simple c++ function I wrote myself. It should just strip
I wrote a simple Android native function that get a filename and some more
I just wrote this simple methode that must return a double value. It's a
I wrote a simple downloader as Java applet. During some tests I discover that
I just found out that I can write a really simple web server using
I'm really confused, I just wrote this x = 0 y = 42 #Can
Just wrote a web service and try to return columns of a sharepoint list
I just wrote a very simple perl tk script to read a huge text
I just wrote some new utility methods within a non-page class for an existing
I just wrote some code to test the behavior of std::equal, and came away

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.