0

I am looking for something similar to what can be seen on the slack Help Center https://get.slack.help/hc/en-us/categories/200111606

They successfully done this, and it appears that each category description from there, has two or three random words which are bolded.

I've tried this:

  //testing random words

  var words = $('.blocks-item-description').text().split(' ');

// with help from http://stackoverflow.com/questions/5915096/get-random-item-from-array-with-jquery

var randomWord = words[Math.floor(Math.random()*words.length)];

// with more help from http://stackoverflow.com/questions/2214794/wrap-some-specified-words-with-span-in-jquery

$('.blocks-item-description').html($('.blocks-item-description').html().replace(new RegExp( randomWord, 'g' ),'<strong>'+randomWord+'</strong>'));

And tested it here: https://doculus.zendesk.com/hc/en-us (not my website) but I was not able to see the same result.

Any help is appreciated.

Thanks

1 Answers1

0

The Slack make bold words that are keywords. E.g.: Sign In to slack

But if you really need to bold random words:

Wrap every word with <span></span> and apply to some of them class for bold.

$(document).ready(function() {
  $('#text').html('<span>' + $('#text').text().replace(/ /g, '</span> <span>') + '</span>');

  for (var i = 0; i < 5; i++) {
    $('#text span').eq(Math.floor(Math.random() * $('#text span').length)).addClass('bold');
  }
});
.bold {
  font-weight: bold;
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="text">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi commodo ante et pharetra iaculis. Nam eget nunc convallis est finibus sollicitudin id at leo. Nullam convallis eros vitae pellentesque molestie. Aliquam facilisis porta lorem in congue. Proin
  ac neque elementum, placerat neque et, dictum ex. Nulla commodo, justo eu lacinia ultrices, sem lectus consequat lacus, sed tempor nibh tellus in enim. Quisque tincidunt odio magna, vel rhoncus mi condimentum eu. Ut nec dolor scelerisque ligula elementum
  pharetra. Fusce rutrum vel ipsum id tristique. Donec vestibulum maximus magna, quis vehicula magna pretium ut. Maecenas vel nunc ex. Proin dui lacus, ullamcorper vitae blandit sed, placerat ac neque. Orci varius natoque penatibus et magnis dis parturient
  montes, nascetur ridiculus mus. Vivamus vel augue at neque aliquet elementum luctus vel erat.
</div>
Justinas
  • 41,402
  • 5
  • 66
  • 96