+ 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
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" ;)
+ 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.
+ 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
+ 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
+ 1
Oh sorry
Anyway fixed it. It still doesn't work why
+ 1
Nevermind they both worked
+ 1
Thanks
+ 1
Just tried your approach worked like a charm
Thank you
+ 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)
0
I just added the code with both examples
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