• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Where to put Javscript? [VSCode]

gothictrade

Well-Known Coder
So I am using VSCode & I have an HTML document and a CSS file. I have a link inside of the HTML index file, linking to the external CSS file.
For JavaScript, I have some lines of code from a tutorial:
JavaScript:
const para = document.querySelector('p');

para.addEventListener('click', updateName);

function updateName() {
  let name = prompt('Enter a new name');
  para.textContent = 'Player 1: ' + name;
}

I'm wondering where exactly I put this?
Do I put it inside of the HTML document? Within which tags? Do I put it in the CSS sheet? Where? With the "p" information? Or do I have an external Javascript Sheet like CSS?
 
So I made a seperate .js file called "javascript.js" and I placed this paragraph of code inside... and I linked to it by putting this in the index.html under <head>:
HTML:
<link rel="javascript" type="javascript" href="javascript.js">

Theres no errors.. however the code doesnt seem to work in the intended way.. or work at all I guess
 
So instead of posting it externally, I removed the link to the external JS file, and I added
<script></script> tags at the beginning of the <body> tag, and I placed the above javascript code into those <script> parameters. However, it still isn't functioning. So I'm wondering if its still an issue with placement or if its actually an issue with the code itself, or if its an issue with firefox that im using - blocking a popup message from being recieved.
 
Glad you got it semi-solved! But take a look at https://codeforum.org/threads/what-is-javascript.117/

You had the idea of linking it externally but the incorrect tag was used (You can click the spoiler below for code, or can go to the link above and learn!)
[CODE lang="html" title="HTML: External JavaScript" highlight="2"]<head>
<script src=”js/javascript.js”></script>
</head>[/CODE]
 
Glad you got it semi-solved! But take a look at https://codeforum.org/threads/what-is-javascript.117/

You had the idea of linking it externally but the incorrect tag was used (You can click the spoiler below for code, or can go to the link above and learn!)
[CODE lang="html" title="HTML: External JavaScript" highlight="2"]<head>
<script src=”js/javascript.js”></script>
</head>[/CODE]

I tried this but it didn't work :( Well, maybe it linked it - however, maybe its a similar issue with the code not being underneath the whatever in the html... so im not sure how i could make that work
 
Top Bottom