Loop through Text Field

Hello,
I’ve a text field $w(‘#text10’) and I want to loop through every word in the text and match it against an existing array.
If the word exists, I want to highlight the word. I tried to use the below code but it doesn’t loop through every word in the Text field.

let emiRed = ['carbon', 'CO2e', 'CO2', 'GHG emisions', 'climate change', 'carbon footprint', 'GHG', 'Paris climate agreement'];
 for(let i = 0; i < emiRed.length; i++) {
 if($w('#text65').text === i) {
     //do something
    }
  }
}

Any help on this will be appreciated. Thank you.

Hello to hello,

this will not work!
You have first to split your TEXT, before you can do a comparisson with a loop.

Take a look at this…

https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_split

If you want a string array, use quotes instead of apostrophes.

let emiRed = ["carbon", "CO2e", "CO2", "GHG emisions", "climate change", "carbon footprint", "GHG", "Paris climate agreement"];

Not positive on the entirety of your question, BUT your for loop is also testing a string value against your increment integer. Unless you parse your .text it’ll fail. What you were prolly trying to do was:


for(let i = 0; i < emiRed.length; i++) {
 if($w('#text65').text === emiRed[i]) {
     //do something
    }}

Adding on to what russian stated, he is correct: when testing your entire text input - you would need to split… otherwise you’d never get matches.