Skip to content

Instantly share code, notes, and snippets.

@jerinisready
Created June 8, 2018 07:05
Show Gist options
  • Select an option

  • Save jerinisready/e01cb318ef85702854779291a19bfd9c to your computer and use it in GitHub Desktop.

Select an option

Save jerinisready/e01cb318ef85702854779291a19bfd9c to your computer and use it in GitHub Desktop.
Sometimes we require to reset a canvas when we want to replace the graph with another set of data. that time simply resigning may cause issue, we can reomove and recreate elements for that purpose
function reset_element(element_id, element_type){
var child = document.getElementById(div_id);
var parent = child.parentElement;
var new_child = document.createElement(element_type);
parent.removeChild(child);
new_child.setAttribute('id', div_id);
parent.appendChild(new_child);
}
// <span id="my_canvas_id"></span>
// <canvas id="my_span_id"></canvas>
reset_element('my_canvas_id', 'canvas');
reset_element('my_span_id', 'span');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment