2

Here is the code that is not working:

// money related syntax (yellow)
var SyntaxmoneyReg = /\b(StartingCurrency|Hello)(?=[^\w])/g

function myFunction() {
  var input = document.getElementById("myInput")
  var data = input.value;
  document.getElementById("demo").innerHTML = "You wrote: " + data;
  data = data.replace(SyntaxmoneyReg, '<span class="code-money">$1</span>');
  input.innerHTML = data;
  document.getElementById("myInput").focus();
}
.code-money { color:blue; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<body>

  <p>Write something in the text field to trigger a function.</p>

  <input type="text" id="myInput" oninput="myFunction()" contenteditable="true">

  <p id="demo"></p>

</body>
</html>

After my "data.replace" detects a match, everytime I enter a letter I lose focus of the textbox.

Shiv Kumar Baghel
  • 2,464
  • 6
  • 18
  • 34
  • Not sure what you mean by detects a match, you might want to elaborate? Or give a sample of what is the match? What I do notice is when you erased the textbox back to empty you lose focus of the textbox. – Mukyuu Dec 13 '18 at 02:17
  • @Mukyuu what i mean is, when you type "Hello" (one of the words its looking for) and then try to type after that, you keep on losing focus – Jaiden Destroyer Dec 13 '18 at 02:21
  • Yeah, I've tried typing "Hello" but I'm not losing focus. I lose focus when I erased text from textbox to empty ("") – Mukyuu Dec 13 '18 at 02:22
  • @Mukyuu did you put a space in between? try "Hello testing123" and you should lose focus – Jaiden Destroyer Dec 13 '18 at 02:24
  • Okay, now it show up – Mukyuu Dec 13 '18 at 02:25

1 Answers1

1

I've moved the </span> from the initial data replace. The problem lies in $1</span>.

Modifying into $i+'</span>' fix the lost focus issue.

To activate your span styling you need to fix your innerHTML. The issue lies in document.getElementById("demo").innerHTML = "You wrote: " + data;. You can remove the document.getElementById("demo").innerHTML = "You wrote: " + data; and input.innerHTML = data;.

Adding var div= document.querySelector('div');

Then modifying div.innerHTML = "You wrote: " + data;

Solve the problem.

You could also remove document.getElementById("myInput").focus(); it was redundant.

Try the following fiddle:

// money related syntax (yellow)
var SyntaxmoneyReg = /\b(StartingCurrency|Hello)(?=[^\w])/g

function myFunction() {
  var input = document.getElementById("myInput");
  var div= document.querySelector('div');
  var data = input.value;
  data = data.replace(SyntaxmoneyReg, '<span class="code-money">$1'+'</span>');
  div.innerHTML = "You wrote: " + data;
}
.code-money { color:blue; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<body>

  <p>Write something in the text field to trigger a function.</p>

  <input type="text" id="myInput" oninput="myFunction()" contenteditable="true">

  <div id="demo"></div>

</body>
</html>
Mukyuu
  • 6,436
  • 8
  • 40
  • 59