Using arrays to change color of certain words in an input box

后端 未结 5 742
温柔的废话
温柔的废话 2021-01-21 18:40

So I\'m working on a JSFiddle and I\'m a little confused about something. I have an input box called myTextField with a random paragraph and a button that calls my change functi

5条回答
  •  猫巷女王i
    2021-01-21 19:26

    So what you could do is search the array of words and compare to the value. Your original code looked to see if the element was an array.

    (function() {
      'use strict';
    
      function change() {
        var matches = ["every", "most", "that", "half", "much", "the", "another", "her", "my", "their", "a", "an", "his", "neither", "these", "all", "its", "no", "this", "any", "those", "both", "least", "our", "what", "each", "less", "several", "which", "either", "many", "some", "whose", "enough", "more", "such", "your"],
          myNewTitle = document.getElementById('myTextField').value,
          title = document.getElementById('title'),
          doesMatch = matches.reduce(word => myNewTitle.search(word) >= -1);
    
        if (doesMatch) {
          console.log('yes');
          title.style.color = 'green';
        } else {
          console.log('no match');
          title.style.color = 'red';
        }
    
        title.innerHTML = myNewTitle;
      }
    
    
      document.querySelector('#byBtn').addEventListener('click', change, false);
    }());
    
    
    
    
      
      
      Using Arrays to change color...
    
    
    
    
      
      
      

提交回复
热议问题