0
Problem with Javascript function
Hi . I made a function in Javascript but i always get a bug . Can you say the problem with my function? Here's the function: function general() { a.style.border = 2px solid red ; a.style.color = "green" ; }
6 ответов
+ 3
Is variable 'a' declared? U might want to find it using a selector query. Also i am pretty sure the 2px solid red line should be in quotes.
0
Yes the variable is declared.
Yeah sorry i forgot quotes . But anyways i still see a bug
0
Seyyed Abbas Hosseini can u provide more info then? Like an error message or full code?
0
Lemme send you the error message:
Uncaught TypeError : Cannot read properties of null ( reading 'style' )
0
in Sololearn, if you're writing the javascript code in the js tab, you have to make sure the html is loaded first. if you just directly try to get a dom element, you will get a null error because the js is executed before the html is loaded.
in the js tab, put your code inside an onload function.
onload = ()=>{
// your code here
}
0
or just put your js in the html tab, before the bottom of the body tags.
<html>
<head>
</head>
<body>
<!-- your html codes -->
<script>
// your js code here
</script>
</body>
</html>