javascript - Displaying Dynamic Content With JS

338

I have a dropdown on a Page as follows, when the user selects a dropdown - I need for a different form to be produced underneath eg:

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

<div id = 'NewContent'>
   //I need different content here depending on selection
</div>

How could I load a different form into the new Content div for each different option a user selects?

to expand on the question say I had a form:

<form id ='TestForm'>
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname">
</form>

how would I use this:

$('select[name="option"]').change(function() {
   $('#NewContent').html(this.value);
});

to add the form as seen above?

588

Answer

Solution:

Try this way : Fiddle link

<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" >Form1</div>
  <div id="form2">Form2</div>
  <div id="form3">Form3</div>
</div>

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

Answer

Solution:

You can use$('#NewContent').html('Some New Content'); to change the content using JQuery

More information is available here.

885

Answer

Solution:

You can use .change() event:

$('select[name="option"]').change(function() {
   $('#NewContent').html(this.value);
});

Fiddle Demo

515

Answer

Solution:

As you mentioned in your post:

I need for a different form to be produced underneath

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

and change the form id like this:

<form id ='Opt1'>...</form>
<form id ='Opt2'>...</form>
<form id ='Opt3'>...</form>

Fiddle

People are also looking for solutions to the problem: php - file size not shown before download start

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.