FedeKrum
FedeKrum

Reputation: 455

My JavaScript button runs only if I don't edit the field's content first

I am trying to make a javascript button that can delete all the spaces in a HTML form field.

It only works once if I don't type inside the field first. I cant figure why.

<html>
<head>
</head>
<body>
  <form>
    <textarea id="gcode" name="gcode" rows="20" cols="100"placeholder="Write your GCODE here...">N100 G20
N102 G0 G17 G40 G49 G80 G90
N104 G91 G28 Z0</textarea>
    <div><input type="button" value="Spaces" onclick="spaces();"></div>
</form>

<script>
function spaces() {
  var str = document.getElementById("gcode").innerHTML;
  var replaced = str.split(' ').join('');
  document.getElementById("gcode").innerHTML=replaced;
};
</script>

</body>
</html>

Upvotes: 0

Views: 38

Answers (2)

user3541974
user3541974

Reputation: 21

Try take the value of textarea input str = document.getElementById("gcode").value; document.getElementById("gcode").value=replaced;

Upvotes: 0

VChiu
VChiu

Reputation: 131

Use value instead

<script>
  function spaces() {
    var str = document.getElementById("gcode").value;
    var replaced = str.split(' ').join('');
    document.getElementById("gcode").value=replaced;
  }
</script>

Upvotes: 1

Related Questions