1

Hi all I have the following dropdown:

<div id='drop'>
    <select name='option'>
        <option value="Opt1">Opt1</option>
        <option value="Opt2">Opt2</option>
        <option value="Opt3">Opt3</option>
    </select>
</div>

<div id = 'NewContent'>
   //I need different content here depending on selection
   <div id="form1" style="display:none">Content of Form1</div>
   <div id="form2"style="display:none">Content of Form2</div>
   <div id="form3"style="display:none">Content of Form3</div>
</div>

I then have the following Javascript:

$('select[name="option"]').change(function() {
    $('#NewContent').find('*').hide();
   $('#'+$(this).val()).show();
});

this will work and display: Content of Form 1, 2 etc. when the user changes the dropdown.

However - the issue is, If I were to simply change one of the divs to include another - nothing displays on the screen, EG.:

  <div id = 'NewContent'>
       //I need different content here depending on selection
       <div id="form1" style="display:none"><div>Content of Form1</div></div> //this won't show  anything 
       <div id="form2"style="display:none">Content of Form2</div>
       <div id="form3"style="display:none">Content of Form3</div>
    </div>

is there a way to get around this issue??

0

2 Answers 2

1

The error is your html.. In your html, whenever there's any change in dropdown "$(this).val()" will always return "Opt1", "Opt2" or "Opt3" and in your html doesn't have any id's with "Opt1", "Opt2" or "Opt3". So, try this:

<div id='drop'>
    <select name='option'>
        <option value="form1">Opt1</option>
        <option value="form2">Opt2</option>
        <option value="form3">Opt3</option>
   </select>
</div>

<div id = 'NewContent'>
//I need different content here depending on selection
   <div id="form1" style="display:none">Content of Form1</div>
   <div id="form2"style="display:none" >Content of Form2</div>
   <div id="form3"style="display:none">Content of Form3</div>
</div>

$('select[name="option"]').change(function() {
   $('#NewContent').children().hide();
   $('#'+$(this).val()).show();
});
Sign up to request clarification or add additional context in comments.

3 Comments

this works however when the page is first loaded all of the Divs appear on the screen at the same time
How's that possible, have you added "style='display: none'" in all divs ?
Ok, check this plnkr, if this is what you want..Plnkr Link
1

Only hide the children, not all descendants (*). By using find('*').hide() you are hiding all descendants and so when you come to show one of the divs, it will be shown but its children remain hidden.

$('#NewContent').children().hide();

1 Comment

Legend - thankyou very much I have been stuck on this for an age!

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.