Question: How to remove any JavaScript code (functions, variables, events etc.) which is Inside a Dom element (div)
Note: This is not similar question like removing content from Dom element.
My Scenario:
My site has a menu. When I click on a menu item I am loading corresponding html file which also contains JavaScript code inside a div element(id=content).
When click on menu I am doing following code
function changeMenu(params){
//need something here to remove previous js codes before load the file
$('#content').load(params['file'],function (){
///ohters code
})
}
It is working fine for first click and executing the JavaScript code as well inside that file.
But problem is it also increase executing JavaScript code number of times I click on the menu. Also when I load another file still previous JavaScript code executing because I had interval function there. Aslo if there is variable declared like this let x=10
it showing follwing error when I click the menu again
Uncaught SyntaxError: Identifier ‘x’ has already been declared
Asking Solution:
Is it posible remove all JavaScript codes when I click on the menu and How?