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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:36:36+00:00 2026-05-30T19:36:36+00:00

jQuery noob here (don’t most posts start like that?) I know I could do

  • 0

jQuery noob here (don’t most posts start like that?)

I know I could do this with CSS alone however, just to make the experience a little more tactile, I’m trying to write a jQuery “spoiler” function that obfuscates a bit of text on a page if it’s marked up appropriately and reveals it on mouseover.

On the whole this works nicely – it replaces the text with some text of your choice and even matches the width of the replaced text to stop the text from reflowing. The problem comes when I add more than one spoiler class on a page – in this case it concatenates the text within each spoiler and seems to match the width of the element first matched element. It’s basically like the function can’t tell the difference between each spoiler so it puts them all together.

I must be doing something wrong on a fairly fundamental level. Can someone point out my mistake please?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Spoiler test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){

    jQuery.fn.spoiler = function(spoilerReplaceText) {
        $(this).addClass('spoilerCensored');
        var spoilerText = $(this).text();
        var spoilerWidth = $(this).width();
        $(this).attr('spoilertext',spoilerText).text(spoilerReplaceText).width(spoilerWidth);
        $(this).stop().hover(
            function(){
                var spoilerText = $(this).attr('spoilertext');
                $(this).fadeOut(250,function(){
                    $(this).text(spoilerText).addClass('spoilerRevealed').removeClass('spoilerCensored').fadeIn(250);
                });
            },
            function(){
                $(this).fadeOut(250,function(){
                    $(this).text(spoilerReplaceText).removeClass('spoilerRevealed').addClass('spoilerCensored').fadeIn(250);
                });
            }
        );
    };

    $('.spoiler').spoiler("don't touch this");
});

</script>

<style type="text/css">
    .spoiler {display: inline-block;background-color: #000000;color: #000000;text-align: center;}
    .spoilerCensored {color: #ffffff;}
    .spoilerRevealed {background-color: #ffffff;color: #000000;}
</style>

</head>

<body>
<p>You can add a <span class="spoiler">spoiler to your content</span> very easily. All you need to do is <span class="spoiler">add a class called spoiler to each bit of text</span> you'd like to hide. There is a problem with <span class="spoiler">adding</span> more than one spoiler to a page though.</p>
</body>
</html>
  • 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-30T19:36:38+00:00Added an answer on May 30, 2026 at 7:36 pm

    you need to treat the argument as a set:

    $.fn.spoiler = function() {
       this.each(function(){
          // yur current code
       });
    
      return this;
    }
    

    If youre using this as plugin which is essentially what you have done you also do want that snippet of code inside document ready. You want it in its own js file which you include just after jquery.

    You also might want to add options/defaults:

    $.fn.spoiler = function(options) {
       var o = $.extend($.fn.spoiler.defaults, options||{});
    
       this.each(function(){
          // yur current code
          // reference the replacement text with o.text
       });
    
      return this;
    }
    
    $.fn.spoiler.defaults = {
       "text": 'Spoiler Alert'
    };
    

    And your usage then becomes:

    $(document).ready(function(){
       $('.spoiler').spoiler({
          'text': 'Custom text'
       });
    });
    

    Check out the plugin authoring docs for more info

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

Sidebar

Related Questions

noob here i got this jquery code that i found on stackoverflow var maxLines
Jquery has a great language construct that looks like this: $(document).ready(function() { $(a).click(function() {
Noob question here, my basic setup looks like this: (function($) { $.fn.imageSlider = function(options)
I'm a jQuery noob here, please help! Much much appreciated! my urls.py: url('^xhr_test/$','posts.views.xhr_test'), my
javascript noob here. So I'm trying to use this s3 uploader jQuery example here
Being jQuery noob I have a problem and I don't know how to solve
I'm a noob in jQuery and have stuck at this. I have the following
I'm a noob at json (know a bit of jquery)....and trying to get a
Im a jquery noob. I needed a script that would swap out images or
I'm a noob with jQuery and I know about scrollTo, but not quite how

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.