Forum Moderators: open

Message Too Old, No Replies

Getting HTML of object before it was modified in JS

         

csdude55

1:31 am on Jan 6, 2024 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Let's say that I have this:

<a href="#">Foo</a>

<script>
$('a').click(() => {
$(this).html('Bar');
});

I just typed that up so it's not tested, but in theory it would change the HTML of the clicked anchor from "Foo" to "Bar".

Is there a built-in way to know the original text (ie, "Foo"), without saving it in a variable beforehand? Something like defaultValue, but for HTML?

In production, I have a somewhat complex JavaScript function that runs when the user clicks, and that function is used by dozens of scripts. But in this ONE script, I'd like to be able to access the default HTML. I can think of several ways to access it if I modify that shared function, but I'd really like to avoid that if I can.