+ 1
jQuery and HTML Data resetting
Okay, so I've been working at this for a couple of hours and got nothing. My problem is simple I think. So I'm using jQuery and AJAX to pull information from a videogame to display the information... which I can do. My problem lies within data presentability. When I click the button I want to reset all the data displayed and then proceed to display the proper data. I'm trying to reset the entire div but leave the html elements within it... how would I go about doing that?
3 Respostas
+ 7
if the elements in the div are all siblings to each other, you can use a class for all of them and then use the class as the selector.
+ 3
If you want to reset values of a <form> elements ( mainly inputs ), without deleting the form itself, you can do:
var myform = document.getElementById('myForm');
var temp = myform.innerHTML;
myform.innerHTML = temp;
When setting the innerHTML property of any element, the source code child of the element is parsing again, so all modification since page load don't be preserved, as for a new load of this part... It's mainly a problem when it occur, but in this case could be helpful ;)
Check this:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script>
function reset() {
var myform = document.getElementById('myForm');
var temp;
temp = myform.innerHTML;
myform.innerHTML = temp;
}
</script>
</head>
<body>
<form id="myForm">
<input type="text" value="default text"><br>
<input type="checkbox" checked>default checked
</form>
<input type="button" value="reset" onclick="reset();">
</body>
</html>
+ 1
And in JQuery, you can replace:
var myform = document.getElementById('myForm');
var temp;
temp = myform.innerHTML;
myform.innerHTML = temp;
... by the JQuery shorthand equivalent:
var temp;
temp = $('#myForm').innerHTML;
$('#myForm').innerHTML = temp;
... if you really want to use JQuery, but in this case, there's no really need, even for readability as for writability, no more for compatibility :P