3

I'm currently using the following to display an input field when I change a dropdown menu selection:

<select id="delivery_country" name="d_country" onchange="
 if (this.selectedIndex==14){
  this.form['statesUSA'].style.visibility='visible'
 }else {
  this.form['statesUSA'].style.visibility='hidden'
 };">

However, this only changes the input form element called "statesUSA".

If I want to show the div that this form element is inside, how do I do this?

For the record, my HTML reads:

<div id="usa"> <input type="text" id="statesUSA" /> </div>

Many thanks for any pointers.

1 Answer 1

1

use document.getElementById(id)

this:

<select id="delivery_country" name="d_country" onchange="if (this.selectedIndex==14){document.getElementById('usa').style.visibility='visible'}else {document.getElementById('usa').style.visibility='hidden'};">
Sign up to request clarification or add additional context in comments.

7 Comments

Hmm. For some reason this isn't working. My current code on the select menu is: if (this.selectedIndex==14){document.getElementById['usa'].style.visibility='visible'}else {document.getElementById['usa'].style.visibility='hidden'};
And the HTML is: <div name="usa" id="usa" class="inputs" style="visibility:hidden;">
don't use [] use parenthesis: () here: instead of ['usa'], use ('usa')
@mcgarriers consider using functions and call them on the onchange
@mcgarriers define a function in a javascript block (like function doThis() { your code here with multile ifs, or maybe case statements } and then use it at the select like this: onchange="doThis()". If the original answer solved your problem, please accept my answer by clicking the √ next to it :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.