Why do some codes work on browsers but not here on sololearn. I am talking about javascript codes. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why do some codes work on browsers but not here on sololearn. I am talking about javascript codes.

On the browser a code like variable.style.display = "block"; works but on sololearn it doesn't On sololearn you have to make it variable.style."display: block;". Why? https://code.sololearn.com/Wy7vlJdQwt4M/?ref=app

30th Apr 2020, 1:21 AM
Addo Frank Ofosu
Addo Frank Ofosu - avatar
11 Answers
+ 5
your code has still some mistakes... HTML: you put "for" attributes to your <button> tags, but they are valid only on <label> tags... a simple workaround in your case and keeping the button apparence would be to set a <label> inside your <button>s: <button><label for="target_id">text</label></button> However, the inverse wont work (button inside label) ^^ JS: rather than your actual if / else if, wich set color but doesn't remove it on unchecked radio, you would better do: n1.style = x1 ? "color:red;" : ""; n2.style.color = x2 ? "red" : ""; "a ? b : c" is called ternary operator syntax and act as "if a then b else c" (in your case "if radio checked set color red else clear style" ;)
30th Apr 2020, 3:51 AM
visph
visph - avatar
+ 3
var n2 = document.getElementById("x2").checked; This is line is incorrect in two ways. I think you might want to solve this problem your self. If not someone will be along soon.
30th Apr 2020, 2:50 AM
ODLNT
ODLNT - avatar
+ 2
the former version should work too Please show your code here so that we can help you https://www.sololearn.com/post/75089/?ref=app
30th Apr 2020, 1:41 AM
Gordon
Gordon - avatar
+ 2
Visph Your way is another method which is really good but my code still works. Anyway I will try it to simplier my work Anyway with the "for" in the button tag, I was using the for when I was using the label tag but I couldn't edit it so I changed to button which can be edited and works. Here is the work of button which thankfully worked. I did alot before getting to here https://code.sololearn.com/W1V56k8j2Moh/?ref=app
30th Apr 2020, 9:36 PM
Addo Frank Ofosu
Addo Frank Ofosu - avatar
+ 1
Oh sorry Anyway fixed it. It still doesn't work why
30th Apr 2020, 3:07 AM
Addo Frank Ofosu
Addo Frank Ofosu - avatar
+ 1
Nevermind they both worked
30th Apr 2020, 3:23 AM
Addo Frank Ofosu
Addo Frank Ofosu - avatar
+ 1
Thanks
30th Apr 2020, 3:24 AM
Addo Frank Ofosu
Addo Frank Ofosu - avatar
+ 1
Just tried your approach worked like a charm Thank you
30th Apr 2020, 9:49 PM
Addo Frank Ofosu
Addo Frank Ofosu - avatar
+ 1
I didn't have said that your code was not working ;) I just suggested some possible improvments ^^ Anyway you're free to do what you want, obviously (and sure, it works like a charm, there nothing difficult there, just basics)
30th Apr 2020, 9:52 PM
visph
visph - avatar
0
I just added the code with both examples
30th Apr 2020, 2:05 AM
Addo Frank Ofosu
Addo Frank Ofosu - avatar
0
But there was a w3 school that I opened on a browser and it worked but when I copied it here, the console was giving a similar issue of the style and when I fixed it. That console went away. Anyway thanks
30th Apr 2020, 3:27 AM
Addo Frank Ofosu
Addo Frank Ofosu - avatar