1

Can you please have a closer look at this source as I don't get any result after submit. Thank you.

<script type="text/JavaScript">
    function reset() {
        document.city.citysearch.value = "";
        return true; 
    }
</script>


<form name='city' action='airport.php' method='post' 
    target='result' onsubmit='return reset()'
>

    <input name="citysearch" type="text" 
        placeholder="Name of the city" size="18">
    <input type="hidden" name="submit" value="search">
Jaak Kütt
  • 2,566
  • 4
  • 31
  • 39
Szetei Zsolt
  • 13
  • 1
  • 4

3 Answers3

1

Seems like incredibly bad practice to have a hidden field named "submit". If you are looking to reset your form fields, try the reset button (http://www.w3schools.com/tags/att_button_type.asp), no JS necessary:

<form name='city' action='airport.php' method='post' target='result' onsubmit='return reset()'>

  <input name="citysearch" type="text" placeholder="Name of the city" size="18">
  <input type="hidden" name="search" value="search">
  <button type="reset">Reset</button>
  <button type="submit">Submit</button>
</form>

UPDATE: Sounds like you want to reset your fields AFTER submission. Do this server-side. Try returning the fields that need reset with the attribute value="".

thelr
  • 1,134
  • 11
  • 30
  • I think he is trying to clear the field after submitting the data. If so I don't think this is the correct solution since the reset button doesn't submit. – Matthew Ertel Feb 26 '13 at 15:47
  • exactly as matthew said .....first the data is submitted and after that it should clear the input field..... – Szetei Zsolt Feb 26 '13 at 15:59
  • Ok. Well, the reset() function you are calling gets executed BEFORE submission, so that's why you don't get anything in submit. – thelr Feb 26 '13 at 16:12
0

Your question is poorly worded, but based off the title I assume you are having problems clearing the citysearch input in the Javascript? If that is the case try something in JS similar to:

var x = document.getElementsByName("citysearch")
x[0].value = ''
Matthew Ertel
  • 198
  • 1
  • 10
-2

Rather do it like this:

  var button = document.getElementById('clear');
  button.addEventListener('click', empty);
  function empty(){
  var input = document.getElementById('text');
  input.value='';
  }
James Baloyi
  • 82
  • 12
  • This won't work (see [this question](https://stackoverflow.com/questions/10693845/what-do-queryselectorall-getelementsbyclassname-and-other-getelementsby-method)). Aside from the omission that stops it from working, your answer is essentially the same as [this one from 2013](https://stackoverflow.com/a/15093490/19068). – Quentin Jun 06 '18 at 08:20
  • http://jsbin.com/kehehumago/1/edit?html,output is a live demo of your code. It does not modify the input value at all. (It sets the `value` property of the node list returned by `getElementsByName` which has no effect). – Quentin Jun 06 '18 at 09:18
  • Running the demo I linked to a second time doesn't produce a different result. – Quentin Jun 06 '18 at 09:33
  • The code in that link doesn't remotely resemble the code in your answer. – Quentin Jun 06 '18 at 09:44
  • The non-working pile of rubbish in your answer is most certainly not worthy of an upvote. – Quentin Jun 06 '18 at 09:46
  • But the logic is the same. What on earth do you have against me? I've updated the answer – James Baloyi Jun 06 '18 at 09:48
  • The logic is not the same! – Quentin Jun 06 '18 at 09:50
  • Congratulations, you have finally edited the answer. It still doesn't work. There is no element with the id `clear` and no element with the id `text`. – Quentin Jun 06 '18 at 09:51
  • He knows what it's supposed to do, the least he can do is play switchi-ditchi with the elements. He can easily add id's – James Baloyi Jun 06 '18 at 09:53