cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <h1>Find Random Words and Add Span Class</h1>
<nav>
   <button class="on">Redact</button>
   <button class="off">Un-Redact</button>
</nav>
<div class="wrapper">
<div id="post-1">
<p>Lorem    ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse lobortis adipiscing felis sed    porttitor. Nunc gravida purus at imperdiet    volutpat. <a href="Curabitur ultricies erat mollis sapien semper adipiscing">Curabitur ultricies erat mollis sapien semper adipiscing</a>. Phasellus elementum nunc eget velit facilisis convallis. Vivamus pulvinar dapibus dui sed pharetra. Praesent mauris turpis, porttitor sed pretium ac, feugiat a nunc. Suspendisse interdum condimentum venenatis. Donec gravida dui sit amet erat laoreet, porttitor sollicitudin tortor condimentum. Integer at venenatis odio. Pellentesque suscipit tellus augue, at dignissim nisi mollis at. Vestibulum a dictum nunc.</p>
</div>
<div id="post-2">
<p>Aenean eget condimentum augue. Sed tincidunt placerat nisi eu ultrices. Quisque in urna at erat venenatis imperdiet. Fusce nunc mi, imperdiet sed porta a, ornare ut justo. < Quisque id odio neque. Mauris dapibus ligula tincidunt sollicitudin bibendum. Pellentesque hendrerit, < lorem non sollicitudin lobortis, elit leo volutpat enim, ac rhoncus orci erat quis dui. Aliquam sed laoreet risus, ut viverra lacus. Vivamus elit massa, porttitor sed nisl in, tincidunt facilisis mauris. Fusce pharetra, lorem ac lobortis dapibus, sapien justo sagittis neque, facilisis faucibus mi augue eu nulla. Fusce vel erat malesuada, commodo nibh ac, pretium lorem. Nulla rhoncus arcu eget volutpat lobortis. Integer semper tellus dui, id bibendum lectus aliquam vel. Vestibulum pulvinar, purus sit amet luctus mollis, ante magna dapibus nibh, in malesuada purus quam ac erat. Morbi hendrerit sodales neque quis blandit.</p>
  
</div>
</div>
            
          
!
            
              body { 
  font-family: sans-serif;
  font-size:14px;
  width: 960px;
  margin:auto;
  background:#fefefe;
} 

nav, h1 {
  text-align:center;
}
 
a {
  text-decoration:none;
}

button {
  cursor: pointer;
  padding:20px;
  border:none;
  border-radius:10px;
  font-size:20px;
  background:#298494;
  color:white;
  text-transform:uppercase;
  width:200px;
}
.redact { 
  background:#000000;
  color:#000000; !important
}

.off {
  display:none;
  background: #bf45bf;
}
            
          
!
            
              $('.on').click(function(){
    $(this).hide();
    $('.off').show();
    // set up a loop to run the function 10 times on click
    var i=0;
    while (i<10) {                    
         
         var data = $('.wrapper').text();
         var arr = data.replace(/[^a-z0-9\s]/gi, '').split(/\s+/);
         var word = arr[Math.floor(Math.random() * arr.length)];
         var fullword = ' '+word+' ';
         // get the text, split in to array, select random word, remove special characters, add spaces around the word to ensure we only find the full word (rather than 'a' inside another word)
         
         console.log(fullword);
         var regex = new RegExp('(?![^<>]*>) *('+fullword+ ')'); //Regular Expression to avoid editing html tags
         $('.wrapper').html($('.wrapper').html().replace(regex,'  <span class="redact">'+word+'</span>  '));
        // highlight word, replacing removed spaces

         i++; // increase loop variable
      
    } // end while
 
});


$('.off').click(function(){
    $(this).hide();
    $('.on').show();
    $('.wrapper').find('span.redact').each(function(index) {
    var text = $(this).text();//get span content
    $(this).replaceWith(text);//replace all span with just content
  });
});

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console