How do I combine javascript with html & css properly

the topic question is the discussion thank you guys for your help

Do you mean how to use link a JS file into a HTML file?

What I mean is how do use javascript to write code that executes upon html & css. Example I create button function with JavaScript how can i make sure it execute properly on html. does that make better sense ?

I am not experienced in using JS in HTML pages but you can link a JS file to a HTML file by using the <script> tag. Learn more here: <script>: The Script element - HTML: HyperText Markup Language | MDN and <script> - SVG: Scalable Vector Graphics | MDN

hi @youngzion

The best practice you can find on the internet usually is importing them externally.

Meaning, having them organized in separate folders. Then, importing into the specific HTML script you’d like the CSS or JavaScript to take effect from.

Why you may ask? This is to help you with readability of the script as you can imagine, if everything been placed under one script, it’ll flood the whole script and might prevent you from performing certain functions in JavaScript, etc.

CSS how to: How to add CSS
JavaScript how to: JavaScript Where To

1 Like

I will look into it, thank you for your efforts

Awesome this reference helped a lot

Here’s a simple example of using the script tag to write a bit of JavaScript code inside the HTML file.
(It is not recommended to write the JavaScript inside the HTML file like this.)

<script> 
/* JavaScript code in here */
function turnTextRed(element) {
  element.style.color = "red";
}
</script>

<button onclick="turnTextRed(this);"> press this button </button>

It turns the text in the button red
(by having the JavaScript do something that changes the button tag to include something like style="color: red;" ) .

2 Likes

That being the case, why post it?