0

I am writing code to clear a particular input field but the button seems to clear it only once.

<div class="input-field col s10">
  <i class="material-icons prefix">web</i>
  <input type="text" disabled name="pages-input" id="pages-input"></input>
</div>
<div class="col s2">
  <input type="button" class="waves-effect waves-light btn red white-text" onClick="document.getElementById('pages-input').setAttribute('value', '');" value="Clear" />
</div>
j08691
  • 204,283
  • 31
  • 260
  • 272
Apoorv Pal
  • 197
  • 3
  • 14
  • FYI there is no `` tag. Inputs are self-closing. But your code works and does what you're asking it to. You can see it if you inspect the underlying code. However you probably want `.value=''` instead of `.setAttribute('value', '')` – j08691 Dec 20 '17 at 15:31
  • `.setAttribute()` ? Shouldn't it just be `.value = ''` ? – David Dec 20 '17 at 15:32
  • why not just use `` and place all your form fields inside a `
    ` tag. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/reset
    – Intervalia Dec 20 '17 at 15:34
  • https://jsfiddle.net/develoger/fwpcjrcj/ – Develoger Dec 20 '17 at 15:56

1 Answers1

0

Inputs are self closing and you must set the value differently.

<div class="input-field col s10">
  <i class="material-icons prefix">web</i>
  <input type="text" disabled name="pages-input" id="pages-input" />
</div>
<div class="col s2">
  <input type="button" class="waves-effect waves-light btn red white-text" 
    onClick="document.getElementById('pages-input').value = '';" value="Clear" />
</div>
Catalin
  • 248
  • 1
  • 2
  • 12
  • Changed it to .value and also sorry about the closing of input tag, that was real foolish of me. But it clears teh field now then I am unable to enter anything in it after this. Also I use JS to add the value to the input from various functions like this. function home_static() { pages.push("Static Home"); //document.getElementById("pages").innerHTML = pages; document.getElementById("pages-input").setAttribute('value', pages); elements = pages.join(' ,') $.post('/quote-backend.php', {elements: elements}) } – Apoorv Pal Dec 21 '17 at 12:57
  • Chnged that JS code to .value too. Working fine now thanks! – Apoorv Pal Dec 21 '17 at 13:15