Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset button to default value for input box

Tags:

jquery

I have an inputbox where visitors can change the value

<input id="custom_coverage" type="text" value="320" name="custom_coverage">

When they change the value, it makes a calculation on the by another jQuery-script.

How can i add a button or a link next to the input box, so visitors can put it back to the default value?

like image 967
jimi Avatar asked Jun 09 '26 02:06

jimi


1 Answers

Store the original value in a data attribute, e.g. <input id="custom_coverage" type="text" value="320" data-original-value="320" name="custom_coverage">

And add a button that switches the values again:

<a href="#" class="restore">Restore</a>

<script>

  $(document).on("click", ".restore", function(){
    var custom_coverage = $("input#custom_coverage");
    custom_coverage.val(custom_coverage.data("original-value"));

  });

</script>

Example: http://jsfiddle.net/deDdy/

like image 190
Richard Avatar answered Jun 10 '26 16:06

Richard