I have 5 radio buttons, every one, when it is pressed, calls the function that hides/shows the input labels that I need.
I used document.getElementById("id").style.display="none";
and it works, but it still takes space.
I want the inputs of the form to fill the space above them when the elements above them are hidden. I think the problem is the <br>
tags to separate the inputs, how can I fix this?
JS function:
JavaScript
x
10
10
1
function showInsert() {
2
document.getElementById("id").style.display="none";
3
document.getElementById("nome").style.display="initial";
4
document.getElementById("provincia").style.display="initial";
5
6
document.getElementById("idLabel").style.display="none";
7
document.getElementById("nomeLabel").style.display="initial";
8
document.getElementById("provinciaLabel").style.display="initial";
9
}
10
HTML Inputs (are in a form if it matters):
JavaScript
1
9
1
<label for="provincia" id="idLabel">ID:</label><br>
2
<input type="text" name="id" id="id" placeholder="ID da ricercare" /><br>
3
4
<label for="provincia" id="nomeLabel">Nome:</label><br>
5
<input type="text" name="nome" id="nome" placeholder="Nome da ricercare" /><br>
6
7
<label for="provincia" id="provinciaLabel">Provincia:</label><br>
8
<input type="text" name="provincia" id="provincia" placeholder="Provincia da ricercare" /><br>
9
Advertisement
Answer
<br>
is meant to be used for line-breaks in text, not for general layouts.
As an alternative, you could set the form to display: flex;
instead:
JavaScript
1
9
1
function showInsert() {
2
document.getElementById("id").style.display = "none";
3
document.getElementById("nome").style.display = "initial";
4
document.getElementById("provincia").style.display = "initial";
5
6
document.getElementById("idLabel").style.display = "none";
7
document.getElementById("nomeLabel").style.display = "initial";
8
document.getElementById("provinciaLabel").style.display = "initial";
9
}
JavaScript
1
5
1
form {
2
display: flex;
3
flex-direction: column;
4
width: 200px;
5
}
JavaScript
1
12
12
1
<form>
2
<label for="provincia" id="idLabel">ID:</label>
3
<input type="text" name="id" id="id" placeholder="ID da ricercare" />
4
5
<label for="provincia" id="nomeLabel">Nome:</label>
6
<input type="text" name="nome" id="nome" placeholder="Nome da ricercare" />
7
8
<label for="provincia" id="provinciaLabel">Provincia:</label>
9
<input type="text" name="provincia" id="provincia" placeholder="Provincia da ricercare" />
10
</form>
11
12
<button onclick="showInsert()">Show Insert</button>