TheMonkeyKing
TheMonkeyKing

Reputation: 337

Select parent div from inside the containing JavaScript

I've a lot of old cached html file which uses document.write. There are multiple element like this in every html file

<div class="time"><script>timeago('2008-06-07 17:40:53')</script></div>

timeago function writes time using document.write and the function is defined in a separate js file. So It is easy to alter the function to some thing else without changing some 200K documents. But the issue is, how can I rewrite that timeago function to select the containing div and print the time. Is it possible to select the div from that script which is contained in that div may be using jQuery or native JavaScript?

Any advice please

Upvotes: 2

Views: 39

Answers (1)

Chase
Chase

Reputation: 3126

How about something like this?

<script>
function timeago(d) {
  document.currentScript.parentElement.innerText = 'output = ' + d;
}
</script>

<div class="time"><script>timeago('2008-06-07 17:40:53')</script></div>

Upvotes: 5

Related Questions