Click here to Skip to main content
14,920,009 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
code for javascript doesn't works sometimes; why is that

HTML
<html>
<head>
<title>Round</title>
<script src="stylejs.js"></script>
<link rel="stylesheet" href="stylecss.css">
</head>
<body>

<div id = "why" background-color = "orange">
Scroll French
</div>

<div class='strings'>AR</div>
<button onclick="myFunction()">Try it</button>

<div class= "robotics" id = "why" width:42px; height:42px background-color = red radius=32px>

<button type="button" name="how" onclick = myFunction()>how are you</button>

<input id="clickWhat" type="button" value="wow" onclick=forFunction(); />

</div>

</body>
<html>


and the js is

JavaScript
document.addEventListener("DOMContentLoaded", function(event) {

var lava = document.getElementById('why');

lava.addEventListener("click", function(){lava.innerHTML = "bonjour"});
});

function forFunction()
{
	console.log("that was great")
}

function myFunction() {
  alert("wow")
}


What I have tried:

online search; bots and others
Posted
Updated 19-Feb-21 9:22am
v4
Comments
Richard Deeming 19-Feb-21 11:55am
   
Nobody can possibly answer such a vague question. Provide proper details of the problem if you want anyone to be able to help you.
four systems 19-Feb-21 12:12pm
   
you know what that is the question; what was working yesterday is not today could post some code
Richard MacCutchan 19-Feb-21 12:17pm
   
What does "not working" mean? People here are not mind readers, despite the excellence of the skills that some of them are blessed with..
four systems 19-Feb-21 12:19pm
   
have posted the code
thatraja 19-Feb-21 12:20pm
   
Include error message in your question always.
four systems 19-Feb-21 12:32pm
   
forFunction() is not defined when wow button is clicked
Patrice T 19-Feb-21 15:19pm
   
Use Improve question to update your question.
So that everyone can pay attention to this information.
four systems 19-Feb-21 12:40pm
   
code changed ; that is the code that worked yesterday but did not today ; that works now but trust me guys there is something out there which doesn't let the code work; any ideas what that is

thanks for replies

Your HTML document is a mess - what is that empty <H1> element doing in the <head>? What are the undefined bg and color attributes on the body? Why are the inline styles on your "robotics" <div> not correctly placed in a style="..." attribute?

Beyond that, you've placed your script in the <head>, so it will execute before the body has been parsed. When it executes, the why element doesn't exist yet, so you'd get an error when you try to add an event listener to it.

You also have an orphaned }); in your script which doesn't match any opening ({, so you'll get a syntax error parsing your script, and the entire script block will be ignored.

You can't just throw random chunks of text at the browser and expect it to magically do what you want! You need to follow the basic rules for structuring your code and your markup.

Work through some basic tutorials on HTML, CSS, and Javascript. Then you might be able to form a coherent document which will work.
   
Comments
four systems 19-Feb-21 12:42pm
   
changed the code that works now but did not earlier its beyond human comprehension why javascript is not consistent
Richard Deeming 22-Feb-21 4:04am
   
No it's not. Although Javascript can been complicated, like any programming language it relies on a fixed set of rules. If you don't follow the rules, it won't work.
thatraja 19-Feb-21 14:28pm
   
5! Wanted to post answer whatever your answer has but I'm too tired & lazy today to post such detailed answer so I skipped this question :D
You have extra characters on line 10 which should not be there. You also have two div items with the same id. So I assume it was working yesterday and then you changed some code and broke it.
   
Comments
four systems 19-Feb-21 12:44pm
   
changed the code that works now but did not earlier ; would keep posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900