H
H
hepkaa2021-07-15 22:20:44
JavaScript
hepkaa, 2021-07-15 22:20:44

Finding changes in text?

How to find changes in text using pure JS?

For example, initially the text was: "Text before change", and after that it became "Text after change". The script will highlight only "before"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TheOnlyFastCoder2, 2021-07-16
@TheOnlyFastCoder2

<span>
  <input id="textarea_1" type="text" placeholder="до"> 
  <input id="textarea_2" type="text" placeholder="после"> 
  <p id="textarea_after"> </p>
</span>
<button id="button">найти</button>

button.onclick = () => {
   textarea_after.textContent = findChanged(textarea_1.value,textarea_2.value)
}

function findChanged (orig,changed) {
  [orig,changed] = [orig,changed].map((text) => text.split(""))
  return changed.filter( (char) =>  orig.indexOf(char) === -1 ).join("")
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question