current file is JS file.
What are you trying to do with html in your js file? I think the code like now is the code used in the .html extension. I think changing the extension to .html will solve the problem.
I think you might be a bit confused. There are a couple of ways to do this, but I’ll give you two for now:
When you want to write HTML in JS file, you have to have an HTML file separate and then target the HTML elements with DOM using javascript.
Another approach is to add in your HTML file and then add your JavaScript logic within it.
This is an example of how you can add javascript to your HTML file. I hope this helps.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic HTML with JavaScript</title>
</head>
<body>
// JavaScript, your calling elements within HTML using DOM
<script>
// Create a button element
const buttonElement = document.createElement('button');
// Set button properties
buttonElement.textContent = 'Click me!';
buttonElement.style.backgroundColor = 'blue';
buttonElement.style.color = 'white';
// Add a click event listener to the button
buttonElement.addEventListener('click', function () {
alert('Button clicked!');
});
// Append the button to the body of the document
document.body.appendChild(buttonElement);
</script>
</body>
</html>
3
Answers
current file is JS file.
What are you trying to do with html in your js file? I think the code like now is the code used in the .html extension. I think changing the extension to .html will solve the problem.
I think you might be a bit confused. There are a couple of ways to do this, but I’ll give you two for now:
When you want to write HTML in JS file, you have to have an HTML file separate and then target the HTML elements with DOM using javascript.
Another approach is to add in your HTML file and then add your JavaScript logic within it.
This is an example of how you can add javascript to your HTML file. I hope this helps.
Happy coding!
change filename test.js to test.html