1

I am writing an application similar to that of a command prompt. It takes userCommand as input & writes the output to a txt id. Im wanting to have it clear the txt area when user submits command "Cls".

HTML:

<div class="Typewriter">
<txt id='output'></txt>
</div>

<input id='cmd' onfocus="clearThis()" size="25" style="font-size:14pt;"/>
<button onclick='text()'>Enter</button>

JS:

function clearThis(){
  if(cmd.value='Cls')
    document.getElementById('output').value='';
  }
}

I have searched high & low but none of the scripts ive found seem to do anything & I cant figure it out. Any help would be much appreciated! TIA

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • 1
    Your javascript syntax is invalid (you have no opening bracket for your if statement but have a closing one). Also, `` is an invalid html element. Lastly, look at when you can calling the `clearThis()` function. I think you can to call it in the `onclick` event – Nick Parsons Nov 05 '18 at 11:50
  • Textarea replaces the output text with a solid blank box where as actually writes the input to the screen so a bit confused on that tbh. But I will try what you have said. Thanks! –  Nov 05 '18 at 12:05

2 Answers2

0

Try <input type="text" id="output"> instead of <txt id='output'></txt>.

mentosp09
  • 123
  • 1
  • 2
  • 11
0

Assuming in command if you write cls then it clean the output and in other cases it will write something in output

Html

<input id='cmd' size="25" style="font-size:14pt;"/>
<button onclick='performOperation()'>Enter</button>

<div class="Typewriter">
<txt id='output'></txt>
</div>

Javascript

function performOperation(){
  if(cmd.value.toLowerCase()=='cls')
    document.getElementById('output').innerText ='';
  else
    document.getElementById('output').innerHTML += cmd.value + "<br/>";
}

Fiddle here

Ranjit Singh
  • 3,715
  • 1
  • 21
  • 35