Convert plain li texts to clickable links with pure javascript, randomly

jzuhri

I have a dynamic list of things. How to select only 3 items (that has 2 words or less) randomly, and convert it to clickable links. So everytimes the page is reloaded, random 3 links are created. Im expecting to use something like:

innerHTML = '<a href="mysite.com/folder/' + url + text.toLowerCase().replace(/\bamp\b|[^A-Z0-9]+/ig, "-") + '">' + text + '</a>'

From this:

  • list 1
  • list 2 222
  • list 3
  • list 4
  • list 5 555
  • list ..
  • list 99
  • list 100
  • To this:

    • list 1
    • list 2 222 (*ignore 3 or more words)
    • list 3 (*selected randomly and pointed to mysite.com/folder/list-3)
    • list 4 (*selected randomly and pointed to mysite.com/folder/list-3)
    • list 5 55 (*ignore 3 or more words)
    • list ..
    • list 99 (*selected randomly and pointed to mysite.com/folder/list-99)
    • list 100
    Aaron Plocharczyk

    Once the page has loaded, loop three times, each time grabbing a random li and refreshing that grab until it meets the criteria of having fewer than three words and not having been used yet. Each time you find an li that is valid, change the innerHTML.

    window.addEventListener('load', (event) => {
      for (var i = 0; i < 3; i++) {
        var randomLI;
        while (!randomLI || randomLI.getElementsByTagName("a").length > 0 || randomLI.innerHTML.trim().split(" ").length > 2) {
          randomLI = rando(document.getElementsByTagName("li")).value;
        }
    
        randomLI.innerHTML = "<a href=\"mysite.com/folder/" + randomLI.innerHTML.toLowerCase().replace(/\bamp\b|[^A-Z0-9]+/ig, "-") + "\">" + randomLI.innerHTML + "</a>";
      }
    });
    <script src="https://randojs.com/1.0.0.js"></script>
    <ul>
      <li>list 1</li>
      <li>list 2 222</li>
      <li>list 3</li>
      <li>list 4</li>
      <li>list 5 555</li>
      <li>list ..</li>
      <li>list 99</li>
      <li>list 100</li>
    </ul>

    I used randojs.com to simplify the randomness and make it more readable. If you want to use this code, make sure this is in the head tag of your html document:

    <script src="https://randojs.com/1.0.0.js"></script>
    

    Collected from the Internet

    Please contact [email protected] to delete if infringement.

    edited at
    0

    Comments

    0 comments
    Login to comment

    Related

    Links in WKWebView randomly not clickable

    Convert HTML to plain text keeping links, bold and italic in Javascript

    Convert jQuery to plain JavaScript

    Convert Angular to plain JavaScript

    find links in plain text and convert them to hyperlinks

    How to wrap texts including inline elements with “p” tag in Plain JavaScript?

    Convert html links inside text to clickable links - SwiftUI

    How to toggle two span texts with pure Javascript, No jQuery please

    How to prevent Outlook from displaying href links as plain texts in email if they are invalid?

    convert JQuery code to Pure Javascript

    add class name in different li by pure javascript

    jquery to pure javascript ul li button click

    Convert a list of URLs into clickable HTML links using ConvertTo-HTML

    How do I randomly apply CSS style using plain Javascript?

    Export Json data to Plain Text using Pure Javascript

    Serverless with TypeScript, convert from plain JavaScript

    How to convert Plain text to Json Object in JavaScript

    Pure Javascript - convert an array into a csv format

    How to convert jQuery code to Pure JavaScript code?

    Convert pure javascript script to a class component in react

    convert jquery each function to pure javascript

    Trying to convert a jQuery plugin to pure javascript

    How to convert this Jquery Ajax to Pure Javascript Fetch?

    Javascript, creating button that randomly selects links inside the page

    Set my Bootstrap navbar links to be active (with pure JavaScript)

    Create ul li from the object using pure javascript

    clickable links in JOptionPane

    Clickable cards with links within

    A href links not clickable or highlightable