JavaScript Conditional Hiding and Showing. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

JavaScript Conditional Hiding and Showing.

I'm confused with JavaScript conditional hiding and showing features, for example see this code:- // I want a program that gets the users input, and if that input matches a specific number I want to hide part of the form. <Html> <Input type="number" id="pin" value=""> <Button type="submit" onclick="hidden()"></button> </Html> <Script> var x=document.getElementById('pin').value; Function hidden(){ If(x.value=1234){ document.getElementById('pin).style.display="none"; } } </Script> Thanks in advance.

19th Jun 2018, 5:56 PM
Cimraan Abdullah
Cimraan Abdullah - avatar
2 Answers
+ 2
var x =document.getElementById('pin'); funxtion hidden() { if(x.value===1234) x.style.display="none"; } First you declared a value on page load which means input is empty u need to remove from variable x and if condition is wrong u not checking you are assigning variable new value which makes this condition always true.
19th Jun 2018, 6:20 PM
ᴋᵘⁿᵃˡ
ᴋᵘⁿᵃˡ - avatar
+ 1
You declared a variable X on the DIRECT value of the input tag, not the ENTIRE element itself, therefore your function should be function hidden(){ var elem=document.getElementById(pin); if(x==1234){ elem.style.display="none"; } }
19th Jun 2018, 6:40 PM
Dlite
Dlite - avatar