0

Suppose I have a script tag :
<script src="https://example.com/test.js"></script>
I want to insert this script tag inside other HTML tags like : form, div, section etc.
So If I insert script tag inside form tag :

<form>
  <script src="https://example.com/test.js"></script>
</form>

https://example.com/test.js should console the closest parent element, which is form.
Similarly, If we insert script tag inside a div, it should console the closest parent element which is div in this case.
Is there any way to achieve this ?

Abhishek kamal
  • 432
  • 1
  • 5
  • 13

1 Answers1

1

In the script test.js you can use:

scriptParent = document.currentScript.parentElement;

or you can use other solutions from How may I reference the script tag that loaded the currently-executing script? and add .parentElement to it.

Filip Hanes
  • 645
  • 4
  • 10