0

My code now ( in same .html file ):

<div id="name">my_name</div>

<script>
    alert($("#name").text());
</script>

this works...

but I want to store this script in a different .js file like...

<script type="text/javascript" src="path/to/name.js"></script>

<div id="name">my_name</div>

expectation to alert 'my_name'

Harsh Jadon
  • 131
  • 1
  • 8
  • 2
    Try to use `ready()` to make a function available after the document is loaded. https://www.w3schools.com/jquery/event_ready.asp or https://learn.jquery.com/using-jquery-core/document-ready/ – Ihor Vyspiansky May 03 '21 at 18:39

2 Answers2

1

It work but you need to use jQuery in function like this :

$(function() {
    alert($("#name").text());
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="https://filebin.net/t8bnvjrt4u4xkezj/test.js?t=2ilgo334"></script>

<div id="name">my_name</div>
The_Death_Raw
  • 1,078
  • 1
  • 8
  • 18
0

My bad..

importing at end works

<div id="name">my_name</div>

<script type="text/javascript" src="path/to/name.js"></script>
Harsh Jadon
  • 131
  • 1
  • 8