user544079
user544079

Reputation: 16639

Equating a variable to "this" in javascript

I am trying to store a table row inside a window variable

window.row = this

where this = <tr><td>Content 1</td><td>Content 2</td></tr>

However, as a class is added to this

<tr class="checked"><td>Content 1</td><td>Content 2</td></tr>

window.row also changes to above.

How can I prevent window.row from being changed each time that the this is changed.

Upvotes: 0

Views: 89

Answers (1)

Oriol
Oriol

Reputation: 288470

If you want row to be the string "<tr><td>Content 1</td><td>Content 2</td></tr>", use outerHTML:

window.row = this.outerHTML;

If you want row to an HTML element like this, use cloneNode:

window.row = this.cloneNode(true); // Use true argument to clone descendants too

Note the clone approach doesn't copy event listeners nor properties.

Upvotes: 2

Related Questions