0

Can I access these values to implement my own custom reset function since I cannot use the standard reset functionality, or do I have to create hidden field to stores these values myself, or can I store the initial data in the client memory somehow as the page is loaded that can then be used by custom reset function.

Update

I have it working for text fields but it doesnt worked for checked values, what am i doing wrong

function resetOptions(divName) {
    var inputs = document.getElementById(divName).getElementsByTagName('input');
    for (i = 0; i < inputs.length; i++) {
        if(inputs[i].type=='checkbox')
        {
            if(inputs[i].defaultChecked==true)
            {
                inputs[i].setAttribute('checked', 'checked');
        }
        else
        {
            inputs[i].removeAttribute('checked');
        }   
    }
    else
    {
        inputs[i].value=inputs[i].defaultValue;
    }   
  } 
 }
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351

1 Answers1

1

You are looking for the properties defaultValue and defaultChecked.

https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • Thanks I guess I will use those, but I am still interested i how the reset() functionality works, do you know ? – Paul Taylor Jan 19 '18 at 11:45
  • And actually maybe I shoudnt not use these since they arent actually the defaults just what the user previosuly had saved last time they used the application. – Paul Taylor Jan 19 '18 at 11:46
  • Huh? Sorry, now you’re adding more confusion, rather than making things more clear. If you output `value` attributes for your form fields based on what the user entered previously, then those _are_ the default values for those fields, by definition. And calling the rest method would restore those, even if the user typed in something different in the meantime. If that is not what you are after, then you’ll have to explain what else actually is. – CBroe Jan 19 '18 at 11:48
  • Perhaps depends on your POV, I would consider defaults to be the values when you first use the application. If the user then changes these values and saves them then the next time the user uses the application these will be the starting values but in my mind they are not the defaults just the current saved values for the user. As I said I cant use default reset() functionality, the reason being my webapge contains 5 tabs with all data in one form, when the user clicks on reset they just expect the current tab to be reset not fields on the other tabs. – Paul Taylor Jan 19 '18 at 12:07
  • _“I would consider defaults to be the values when you first use the application”_ - HTML can not possibly “know” anything about that, so in an HTML context that makes very little sense. Default values are the values the form fields had set in the HTML code when the document was loaded. If you want to “restore” anything else, then you will have to make that anything available to your script in the first place. Custom data attributes, as already mentioned by Sam Axe in comments, would be an easy way to achieve that. – CBroe Jan 19 '18 at 12:11
  • Okay I agree with you so about the Html context POV, so I can use defaultValue and defaultChecked and then use javascript function to reset instead of using form reset because of my tab problem (and maybe an applicationDefault data attribute for the other case). I'm still interested in how the actual reset() remembers the starting value though ? – Paul Taylor Jan 19 '18 at 12:27
  • Well the browser/DOM engine stores those values internally ... – CBroe Jan 19 '18 at 12:38
  • Its not visible to me ? – Paul Taylor Jan 19 '18 at 13:04
  • “Visible” to you, when you go look it up in the mentioned properties ... – CBroe Jan 19 '18 at 13:14
  • I dont understand you, standard reset() works without me putting anything into defaultValue(), but are you saying it uses these implicitly even if you dont set it yourself? – Paul Taylor Jan 19 '18 at 13:21
  • You don’t need to set them, the browser does that for you when he parses the HTML and creates the element. – CBroe Jan 19 '18 at 13:23
  • oh I see, they are always already there even if not visible in html, right okay that wasn't clear to me, but that answers my question. – Paul Taylor Jan 19 '18 at 13:29
  • Yes, they are DOM properties, not actual HTML attributes. – CBroe Jan 19 '18 at 13:30
  • Hmm, Im not clear on the difference – Paul Taylor Jan 19 '18 at 13:36
  • http://joji.me/en-us/blog/html-attribute-vs-dom-property, https://stackoverflow.com/questions/6003819/what-is-the-difference-between-properties-and-attributes-in-html should be able to shed some light on that issue. – CBroe Jan 19 '18 at 13:39
  • Taking your advice i have it working for text fields and have updated question but doesnt work for checked values, can you help ? – Paul Taylor Jan 23 '18 at 08:04