3

i tried the following example and my aim is to achieve that if i press the reset button, only the field in the current line will be reseted, is this possible with only html5 and no jquery/ javascript ? any ideas are welcome :)

<form action="fieldset_legend.htm">
  <fieldset>
    <legend>Absender</legend>

        <label for="vor">Ihr Vorname:</label>
        <input id="vor" size="40" maxlength="40" name="Vorname" type="text">
        <input type="reset" for="vor" value="reset">

        <label for="nach">Ihr Zuname:</label>
        <input  id="nach" size="40" maxlength="40" name="Zuname" type="text">
        <input type="reset" for="nach" value="reset">

  </fieldset>
</form>
Dominik Zinser
  • 136
  • 2
  • 10

3 Answers3

1

Use javascript...

function resetInput(id) {
   document.getElementById(id+"input").value="";
}

HTML:

    <label for="vor">Ihr Vorname:</label>
    <input id="vorInput" size="40" maxlength="40" name="Vorname" type="text">
    <input type="button" id="vor" onclick="resetInput(this.id) "value="reset">

    <label for="nach">Ihr Zuname:</label>
    <input  id="nachInput" size="40" maxlength="40" name="Zuname" type="text">
    <input type="button" id="nach" onclick="resetInput(this.id) value="reset">
roee
  • 41
  • 8
0

I don't believe there is a way to avoid JavaScript in this case as the input 'reset' will reset for all the elements in the form.

reset: A button that resets the contents of the form to default values.

Input elements

Doron Sinai
  • 1,166
  • 3
  • 12
  • 28
0

I found a workaround what works for me.

You need to put your fields into nested forms e.g. like this:

<form action="fieldset_legend.htm">
  <fieldset>
    <legend>Absender</legend>
      <form id="line1">
        <label for="vor">Ihr Vorname:</label>
        <input id="vor" size="40" maxlength="40" name="Vorname" type="text">
        <input type="reset" for="vor" value="reset">
      </form>

      <form id="line2">
        <label for="nach">Ihr Zuname:</label>
        <input  id="nach" size="40" maxlength="40" name="Zuname" type="text">
        <input type="reset" for="nach" value="reset">
      </form>

  </fieldset>
</form>

The reset button will only reset the "closest" form it finds, in this case it will reset only the "line1" form for the first button and the line2 form the other button.

EDIT:

I found articles where people are calling nested forms a "hack" (like this one ), because it seems not to be part of the browser standards.

So keep in mind, that this nested forms solution, could cause unexpected behavior in some browsers, because this is currently not part of the standards (even if this works since decades of years)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Radon8472
  • 4,285
  • 1
  • 33
  • 41
  • to be noted as this could impact layout/styling: the 1st `
    ` (here `id="line1"`) will not be rendered in the dom when doing so, the children of the form will be directly put at its place.
    – y_nk Aug 17 '23 at 03:48