1

document.getElementsByTagName('span')[0].onclick = function(ev) {
  this.innerHTML, this.parentNode.id = "Z";
 
  //^^^^^^^^^^^^^^^^^^^^^
  //this.innerHTML = "Z";
  //this.parentNode.id = "Z";
}
#Z {
  color: red;
}
<div>
  <span>x</span>
</div>

i want to apply the value "Z" in to this.innerHTML, this.parentNode.id in a single line instead of two lines but it doesn't work using comma

Joe Doe
  • 523
  • 2
  • 9
  • Don't compress your developement code, let a compressing tool do it for the production code. – Teemu Jul 09 '19 at 17:50

1 Answers1

2

Just chain the assignments.

Maybe worth a look: In JavaScript, is chained assignment okay?

The comma operator has a different purpose and result.

document.getElementsByTagName('span')[0].onclick = function(ev) {
    this.innerHTML = this.parentNode.id = "Z";
}
#Z { color: red; }
<div><span>x</span></div>
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392