I believe with Code Pen it is automatically connected. But in an application not on Code Pen you would either write inline JS using the script tags or use the script tag to point to the JS file.
<script src='yourJSFile.js'></script>
<!-- OR -->
<script>
//Some JS here
const yourVariable = document.querySelector('h1'); //Not the best way to do this
yourVariable.style.fontFamily = 'arial';
</script>
its already connected so the script tag becomes completly unecearry
this is not really relevant at all. thanks for the trying to :3 but i need something that is actually usefull
The only thing you told me was how to connect a js to a html file which was completly irrelevant
then you state: Not the best way to do this. Why it is not a good option it doesn’t say, any better option it doesn’t give. So what’s the point then. If it’s only a halfbacked answere
sorry if it sound rude. i know you want to help and have good intentions.
So I did show you how to do that. I put it wasn’t the best way to do it because you were referencing styling in your question. Something like this would be done in CSS. If you really wanted it done in JS I would add a class to it and get the class from the DOM rather than the element in the HTML.
This is how you can add styles from your JS to an element from your HTML.
yourVariable.style.fontFamily = 'arial';
which I also showed you how to do in my original post.
if you know DOM (document object model) JS, then you can code in the JS editor on the bottom of the others.
If you want to do all of the stuff in the HTML editor, then you can wrap your code in <script> tags.
If you are using a library like react, for instance, then use two script tags:
<script src = "[library link]"></script> <!--no code in the first set of script tags-->
<script>
//add your JS!!
</script>
yes but only with the h1 element. what about when i use a p one our a div/something else
do i need to type out the entire:
const yourVariable = document.querySelector('h1')
each time?
also it wasn’t about styling but placing the js at a specific place into my document using the html/css to guide it to it since they never seem to talk about placing and givng the js a specific coordinate to a page. and i simpley want to connect for example a time and date to it when i want it onto a specific place?
the dom just referse to document object model which seems more of a styling guide how to place stuff how is it ussefully excatfully?
i am asking how to do without the script tags connect them to one another wiithhout them being in the same editor.
you don’t understand what i am asking at all.
no css in html no js into html
thats to messy and confusing
no script tags involved because codepen makes them uneceary
each one into its own designated area
the short explanation is that you need to select an html element before you can do any changes to it, in the same way you need to select an html element to apply css style to it
Okay so I tried to connect HTML with JS
Following the guide onto w3schools
Bellow is all the things i tried so far
and so far none of them seem to work
you should not put <script> tags in the JS part, as everything in there is already automatically put in a script tag. remove the script tags, and you will not have the lines there appearing as text in the Result tab. after that you will see what’s working and not working
don’t add HTML in the JS part, that also gives errors and stop things from working
works? is it because it needs to includes the innerHTML tag?
when i look at the example which they did with a paragraph
it connects but when i change it to a button element like bellow
and give it the id which should connect to the id of the button id in html it fail
why is that?
<button id="button"></button>
var myElement = document.getElementById("button");
<button type="button" onclick="myFunction()">Try it</button>
If you’re working IN codepen, you don’t have to worry about adding script tags anywhere. You can simply just put your JS over in the appropriate window. Secondly you’re basically trying to target the button two different ways at the same time, which isn’t going to work. I’ve cleaned up your code in my fork and added some comments to hopefully clear some things up.
I’ve also added a second way of targeting and manipulating the DOM (see my second example in the codepen) using .textContent rather than .innerHTML. For the purposes of what you’re trying to do, it’s a better way to go about it. Usually you should only use innerHTML if you’re going to be changing the actual code of an element or adding code to it. Just be aware it could be liable for code injection depending on what you’re doing with it.