You can use the HTML5 data- attributes to implement the return function. Thus, each <input> will have its original value if the revert button is used.
Here's what it looks like:
<table> <tr> <td><input type='text' value='change me' data-original='change me' /></td> <td><input type='text' value='change me2' data-original='change me2' /></td> <td><input type='button' value='revert' onclick='revert(this)'/></td> </tr> <table>
And the code that is returned:
function revert(btn) { var parentTr = btn.parentNode.parentNode; var inputs = parentTr.getElementsByTagName('input'); for(var i = 0; i < inputs.length; i++) { if (inputs[i].type == 'text') { inputs[i].value = inputs[i].getAttribute('data-original'); } } }
The data-original attribute can be generated:
As a side solution, you can save the original values ββin the map object . Here (3) is a demo for this (notification I added an id for each input , so it can be used as a key to map ).
Keep in mind that neither solutions (2) nor (3) require changing the code on the server side (3 if your inputs have identifiers). And (2) feels more clear.
About the defaultValue attribute:
The defaultValue attribute can only be a solution if the return value never changes and if the text input s fields are involved.
Firstly, changing the "default" is rather inconvenient and might break something else when using the page (one would expect browsers to use the defaultValue attribute as defaultValue only, but that doesn't seem to be the case). Secondly, you will be limited to the input text type.
However, if this is not a problem, the above code can be quickly adapted for use instead of data- attributes.
acdcjunior
source share