Younes
Younes

Reputation: 35

How can I deactivate images with an onClick Event using JavaScript ? (Working on browser extension)

popup.html looks like this:

<div id="no-img" onClick="noImg()">
    <p> Disable images </p>
</div>

background.js (Thats how I tried but it doesn't work) :

function noImg () {
var disable = document.getElementsByTagName('img');
for (var i=0; i < eles.length; i++)
disable[i].onclick = null;
}

How can I make all the images on a page disabled by clicking on the paragraph tag and enabled by clicking again?

Thanks

Upvotes: 3

Views: 31

Answers (1)

JSchirrmacher
JSchirrmacher

Reputation: 3362

You need to assign the previous value to another attribute to swap it back later, for example

function noImg () {
  var elems = document.getElementsByTagName('img'),
      swap;

  for (var i=0; i < elems.length; i++) {
    swap = elems[i].onclick;
    elems[i].onclick = elems[i].saveclick;
    elems[i].saveclick = swap;
  }
}

Upvotes: 1

Related Questions