max
max

Reputation: 3716

removing script element and it's actions/events from page

i have a script element in my web which is not in my header ( i don't know if thats important )

script.js

 $(function(){
    $('.test').click(function(){
      alert(' this button was clicked ! ');
    })
 })

i need it to be removed from my page

var script = $('script[src$="js/script.js"]') ;
if(sript.length == 0 )
{
  alert(" script cannot be found  ");
}
else
{
  script.remove();
  alert(" done ! ");
}

but after this still i can see the script in my source code and when i click the button still i get the message

i don't want to unbind the click event for that button . it's just an example i want the script and all of it's events gone

Upvotes: 1

Views: 195

Answers (1)

zerkms
zerkms

Reputation: 254944

You just need to unbind the event handler like:

$('.test').off('click')

If you have several handlers for click attached - then you need to use namespaces to have more fine grained control over binding/unbinding

Upvotes: 2

Related Questions