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" ; }

29th Jul 2025, 9:45 AM
Seyyed Abbas Hosseini
Seyyed Abbas Hosseini - avatar
7 Antworten
+ 1
or just put your js in the html tab, before the closing body tag. <html> <head> </head> <body> <!-- your html codes --> <script> // your js code here </script> </body> </html>
29th Jul 2025, 1:08 PM
Bob_Li
Bob_Li - avatar
+ 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.
29th Jul 2025, 9:48 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 1
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 }
29th Jul 2025, 1:04 PM
Bob_Li
Bob_Li - avatar
+ 1
Oh i got it . Thank you very much
29th Jul 2025, 1:15 PM
Seyyed Abbas Hosseini
Seyyed Abbas Hosseini - avatar
0
Yes the variable is declared. Yeah sorry i forgot quotes . But anyways i still see a bug
29th Jul 2025, 11:33 AM
Seyyed Abbas Hosseini
Seyyed Abbas Hosseini - avatar
0
Seyyed Abbas Hosseini can u provide more info then? Like an error message or full code?
29th Jul 2025, 11:53 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
0
Lemme send you the error message: Uncaught TypeError : Cannot read properties of null ( reading 'style' )
29th Jul 2025, 11:58 AM
Seyyed Abbas Hosseini
Seyyed Abbas Hosseini - avatar