imjustaguy
imjustaguy

Reputation: 43

Set multiple variables .onload to the same value

I have this function:

window.onload = function(){ 
  var rows = document.querySelector('.rows');
  var columns = document.querySelector('.columns');
  rows.onpaste = function(){return false;}
  columns.onpaste = function(){return false;} 
  rows.ondrop = function(){return false;}
  columns.ondrop = function(){return false;} 
}

is there any way that I can set all .ondrop and .onpaste to the same function in one line of code?

Upvotes: 1

Views: 136

Answers (3)

Keff
Keff

Reputation: 1071

If you can use arrow function you can do it like this

var rows = document.querySelector('.rows');
var columns = document.querySelector('.columns');

rows.ondrop = columns.ondrop = () => false
rows.onpaste = columns.onpaste = () => false

Upvotes: 0

Zohaib Ijaz
Zohaib Ijaz

Reputation: 22885

You can do this in one line like this

rows.ondrop = columns.ondrop = function(){return false;} 

Upvotes: 1

Alex Santos
Alex Santos

Reputation: 2950

Yes, a simple way (although I don't think it's the nicest) would be something like this:

window.onload = function(){ 
  var rows = document.querySelector('.rows');
  var columns = document.querySelector('.columns');
  columns.ondrop = rows.ondrop = columns.onpaste = rows.onpaste = function(){return false;} 
}

Upvotes: 2

Related Questions