1

I am trying to change the color of a number in a text file when the number gets below a certain point. this file is read in with javascript. For example, if the number is 400, the text color should turn green. If the number is 200, the text color should turn red

I have tried to use the fontcolor() function and have not had any luck. The code that I have provided just basically shows the content of the text file inside the iframe. I want to be able to change the color of the text inside of the iframe

<iframe id="textfile" src="john.txt"></iframe>

<script type="text/javascript">
  function print() {
      var iframe = document.getElementById('textfile');
      iframe.contentWindow.print();
  }
</script>

For example, if the number is 400, the text color should turn green. If the number is 200, the text color should turn red

Masuk Helal Anik
  • 2,155
  • 21
  • 29
John Fore
  • 41
  • 4
  • I dont think thats possible as JavaScript is a client-based i.e. browser based language and doesn't have access to the client's file system. However, if you were to do this using Node.js this is possible – Ajay Gupta Apr 25 '19 at 17:02
  • Where are you reading in the file with JavaScript? Where is your call to `fontcolor()`? – Barmar Apr 25 '19 at 17:02
  • I think `fontcolor()` only works in HTML documents. It apparently wraps the text in a `` element. – Barmar Apr 25 '19 at 17:02

1 Answers1

1

Do not use the iframe. Read the text file with javascript and then put all data in a div, so you can change the color of 400 with html font tag or css.

Just an idea, after the reading data from txt file you can search 400 key in string and after that you can change it <font color="green">400</font> before putting in div..

Cotur
  • 450
  • 2
  • 10
  • Could you provide an example of using div? I am fairly new to Javascript and HTML – John Fore Apr 25 '19 at 17:12
  • Check these please, they'll will help; [Read a file](https://stackoverflow.com/questions/14446447/how-to-read-a-local-text-file) , [Writing text in a div](https://stackoverflow.com/questions/7195844/writing-text-on-div-using-javascript) and [Replace text](https://www.w3schools.com/jsref/jsref_replace.asp) – Cotur Apr 25 '19 at 17:17