0

So I have this code that creates new text areas, I was hoping someone could show me how to change the code so it creates a new drop down list instead. The content for the list will be the same in every box.

I think its a case of a few minor changes to the javascript.

JAVASCRIPT

        var counter = 0;

  function addNew() {

// Get the main Div in which all the other divs will be added
var mainContainer = document.getElementById('mainContainer');

// Create a new div for holding text and button input elements
var newDiv = document.createElement('div');

// Create a new text input
var newText = document.createElement('input');
newText.type = "text"; 

//for testing
newText.value = counter++;

// Create buttons for creating and removing inputs
var newAddButton = document.createElement('input');
newAddButton.type = "button";
newAddButton.value = " + ";

var newDelButton = document.createElement('input');
newDelButton.type = "button";
newDelButton.value = " - ";

// Append new text input to the newDiv
newDiv.appendChild(newText);

// Append new button inputs to the newDiv
newDiv.appendChild(newAddButton);
newDiv.appendChild(newDelButton);

// Append newDiv input to the mainContainer div
mainContainer.appendChild(newDiv);

// Add a handler to button for deleting the newDiv from the mainContainer
newAddButton.onclick = addNew;

newDelButton.onclick = function() {
        mainContainer.removeChild(newDiv);
 };
}

HTML

             <select name="text">
                <option value="t1">t1</option>
                <option value="t2">t2</option>
                <option value="t3">t3</option>
              </select>
              <input type="button" value=" + " onClick="addNew();">
            </div>
1

1 Answer 1

1

Great question, here is a solution you can implement pretty easily given your existing code.

Instead of (part of your existing code):

// Create a new text input
var newText = document.createElement('input');
newText.type = "text"; 

//for testing
newText.value = counter++;

// Append new text input to the newDiv
newDiv.appendChild(newText);

Use:

// create dropdown element and one option element
var newDropdown = document.createElement('select'),
    newDropdownOption = document.createElement("option");

// assign 'value' and 'text' properties to the option element   
newDropdownOption.value = "value1";
newDropdownOption.text = "option 1";

// add the option to the dropdown DOM node
newDropdown.add(newDropdownOption);

// add dropdown to mainContainer
newDiv.appendChild(newDropdown);

Hope that helps!

EDIT: here's the full working code. you should be able to take it from here!

function addNew() {

            // Get the main Div in which all the other divs will be added
            var mainContainer = document.getElementById('mainContainer');

            // Create a new div for holding text and button input elements
            var newDiv = document.createElement('div');

            // Create a new text input
            var newDropdown = document.createElement('select');

            newDropdownOption = document.createElement("option");
            newDropdownOption.value = "value1";
            newDropdownOption.text = "option 1";

            newDropdown.add(newDropdownOption);

            // Create buttons for creating and removing inputs
            var newAddButton = document.createElement('input');
            newAddButton.type = "button";
            newAddButton.value = " + ";

            var newDelButton = document.createElement('input');
            newDelButton.type = "button";
            newDelButton.value = " - ";

            // Append new text input to the newDiv
            newDiv.appendChild(newDropdown);

            // Append new button inputs to the newDiv
            newDiv.appendChild(newAddButton);
            newDiv.appendChild(newDelButton);

            // Append newDiv input to the mainContainer div
            mainContainer.appendChild(newDiv);

            // Add a handler to button for deleting the newDiv from the mainContainer
            newAddButton.onclick = addNew;

            newDelButton.onclick = function() {
                    mainContainer.removeChild(newDiv);
            };
        }

Adding HTML: I've been going off of the HTML you provided, so this should work just fine.

<div id="mainContainer">
    <select name="text">
        <option value="t1">t1</option>
        <option value="t2">t2</option>
        <option value="t3">t3</option>
    </select>
    <input type="button" value=" + " onClick="addNew();">
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

Cheers for your reply, I haven't been able to create the changes correctly, the button no longer works with the new code.
gotcha -- i've added the full working code to my reply. good luck!
Thanks for the help, but can you send the file or link for it working, as there is no action on the click of the button. Thankyou for your help up to now

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.