-1
wanted help with this code. I made it and it works, but I wanted to dry it because it’s very verbose and ugly. I’m a beginner in javascript. The single quotes are because the code goes inside a PHP ECHO. Note: I don’t want to do with jQuery.
Thanks in advance for the help!
function filtro(){
getValue = document.getElementById('selecao').value;
if(getValue == ''){
document.getElementById('ano_grau').style.display = 'none';
document.getElementById('curso').style.display = 'none';
document.getElementById('cargo').style.display = 'none';
document.getElementById('avaliacao').style.display = 'none';
document.getElementById('disponibilidade').style.display = 'none';
}
if(getValue == 'ano_grau'){
document.getElementById('ano_grau').style.display = 'block';
document.getElementById('cargo').style.display = 'none';
document.getElementById('curso').style.display = 'none';
document.getElementById('avaliacao').style.display = 'none';
document.getElementById('disponibilidade').style.display = 'none';
}
if(getValue == 'curso'){
document.getElementById('curso').style.display = 'block';
document.getElementById('cargo').style.display = 'none';
document.getElementById('ano_grau').style.display = 'none';
document.getElementById('avaliacao').style.display = 'none';
document.getElementById('disponibilidade').style.display = 'none';
}
if(getValue == 'cargo'){
document.getElementById('cargo').style.display = 'block';
document.getElementById('curso').style.display = 'none';
document.getElementById('ano_grau').style.display = 'none';
document.getElementById('avaliacao').style.display = 'none';
document.getElementById('disponibilidade').style.display = 'none';
}
if(getValue == 'avaliacao'){
document.getElementById('ano_grau').style.display = 'none';
document.getElementById('curso').style.display = 'none';
document.getElementById('cargo').style.display = 'none';
document.getElementById('avaliacao').style.display = 'block';
document.getElementById('disponibilidade').style.display = 'none';
}
if(getValue == 'disponibilidade'){
document.getElementById('ano_grau').style.display = 'none';
document.getElementById('curso').style.display = 'none';
document.getElementById('cargo').style.display = 'none';
document.getElementById('avaliacao').style.display = 'none';
document.getElementById('disponibilidade').style.display = 'block';
}
}
<select id='selecao' onChange='filtro()'>
<option value='' selected>Selecione uma opção</option>
<option value='ano_grau'>Ano em que colou grau</option>
<option value='curso'>Curso realizado depois de formado</option>
<option value='cargo'>Cargo atual</option>
<option value='avaliacao'>Avaliação entre a formação e a exigência do cargo</option>
<option value='disponibilidade'>Disponibilidade de colaboração com a faculdade por meio de entrevistas e palestras</option>
</select>
Your code above seems to be incomplete...
– Sorack
i put HTML just to show that this is a select. I really want to dry javascript.
– Wanderson Silva
Without the full code I won’t be able to help you, otherwise I’ll have to kick an answer.
– Sorack
Whenever posting a question, it is important to explain the difficulty encountered and always provide a [mcve] of the problem. To understand what kind of question serves the site and, consequently, avoid closures and negativities worth reading What is the Stack Overflow and the Stack Overflow Survival Guide (summarized) in Portuguese.
– Bacco