dimanche 2 décembre 2018

Running Javascript "getValue" twice for optional (hidden forums)

I have a few forms that I need to be hidden until a user clicks an option. I am sadly a total newbie with Javascript. I found this code on SO, and it works great for 1 field, but when I try to do multiple, it breaks the fields (only one works).

<script type="text/javascript">
function getValue(x) {
if(x.value == 'custom'){
document.getElementById("yourfield").style.display = 'block';
 }
 else{

document.getElementById("yourfield").style.display = 'none';
}
}
</script>

I tried changing the fields and the value (and adding the 2 scripts on top of each other). But one of the fields will not show.

<script type="text/javascript">
function getValue(x) {
if(x.value == 'othercustom'){
document.getElementById("otherfield").style.display = 'block';
 }
 else{

document.getElementById("otherfield").style.display = 'none';
}
}
</script>

I also tried using an else if statement but I don't think this is how else if works in JS, and of course, this code does not work.

<script type="text/javascript">
function getValue(x) {
if (x.value == 'othercustom'){
document.getElementById("otherfield").style.display = 'block';
}
else{
document.getElementById("otherfield").style.display = 'none';
} else if (x.value == 'custom'){
      document.getElementById("yourfield").style.display = 'block';
} else{
    document.getElementById("yourfield").style.display = 'none';
}
}
</script>

Any help would be appreciated. Thank you.

Aucun commentaire:

Enregistrer un commentaire