Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Predictive Text</title>
    <!-- Google Font -->
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap"
      rel="stylesheet"
    />
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="input-container">
      <input
        type="text"
        id="input"
        placeholder="Type something here.."
        autocomplete="off"
      />
      <span id="suggestion"></span>
    </div>
    <!-- Script -->
    <script src="script.js"></script>
  </body>
</html>
              
            
!

CSS

              
                * {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}
body {
  background-color: #2c8df6;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
.input-container {
  position: relative;
  background-color: #ffffff;
  width: 25em;
  height: 4.4em;
  border-radius: 5px;
}
input {
  outline: none;
  border: none;
  background-color: transparent;
  position: absolute;
  width: inherit;
  height: inherit;
  color: #000000;
  font-size: 25px;
  padding: 0 18px;
  z-index: 3;
}
#suggestion {
  width: inherit;
  height: inherit;
  position: absolute;
  z-index: 2;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  padding: 0 18px;
  font-size: 25px;
  color: #868686;
}
@media screen and (max-width: 600px) {
  .input-container {
    width: 80vw;
  }
}
              
            
!

JS

              
                let words = [
  "Apple",
  "Pencil",
  "Pen",
  "Chair",
  "Helmet",
  "Grapes",
  "Tub",
  "Trophy",
  "Cookie",
  "Donut",
  "Shirt",
  "Bat",
  "Ash",
  "Bell",
  "Chat",
  "Ball",
  "Eye",
  "Fish",
  "Zip",
  "Game",
  "Juice",
  "Orange",
  "Fan",
  "Ice",
];
words.sort();
let input = document.getElementById("input");
let suggestion = document.getElementById("suggestion");
//Enter key code
const enterKey = 13;

window.onload = () => {
  input.value = "";
  clearSuggestion();
};

const clearSuggestion = () => {
  suggestion.innerHTML = "";
};

const caseCheck = (word) => {
  //Array of characters
  word = word.split("");
  let inp = input.value;
  //loop through every character in ino
  for (let i in inp) {
    //if input character matches with character in word no need to change
    if (inp[i] == word[i]) {
      continue;
    } else if (inp[i].toUpperCase() == word[i]) {
      //if inp[i] when converted to uppercase matches word[i] it means word[i] needs to be lowercase
      word.splice(i, 1, word[i].toLowerCase());
    } else {
      //word[i] needs to be uppercase
      word.splice(i, 1, word[i].toUpperCase());
    }
  }
  //array to string
  return word.join("");
};

//Execute function on input
input.addEventListener("input", (e) => {
  clearSuggestion();
  //Convert input value to regex since string.startsWith() is case sensitive
  let regex = new RegExp("^" + input.value, "i");
  //loop through words array
  for (let i in words) {
    //check if input matches with any word in words array
    if (regex.test(words[i]) && input.value != "") {
      //Change case of word in words array according to user input
      words[i] = caseCheck(words[i]);
      //display suggestion
      suggestion.innerHTML = words[i];
      break;
    }
  }
});

//Complete predictive text on enter key
input.addEventListener("keydown", (e) => {
  //When user presses enter and suggestion exists
  if (e.keyCode == enterKey && suggestion.innerText != "") {
    e.preventDefault();
    input.value = suggestion.innerText;
    //clear the suggestion
    clearSuggestion();
  }
});
              
            
!
999px

Console