How to add select in javascript - javascript

I am trying to add option in select tag but its not working.
container.appendChild(document.createTextNode("location"));
var select = document.createElement("select");
var option1 = document.createElement("option");
option1.text = "mumbai";
option1.value = "mumbai";
container.appendChild(option1);
container.appendChild(select);

You're adding the option to the container and not to the select. So change container.appendChild(option1); to select.appendChild(option1);:
var container = document.getElementById('elem')
container.appendChild(document.createTextNode("location"));
var select = document.createElement("select");
container.appendChild(select);
var counter = 0;
document.getElementById("btn").addEventListener("click", function() {
counter++;
var option1 = document.createElement("option");
option1.text = "mumbai" + counter;
option1.value = "mumbai" + counter;
select.appendChild(option1);
});
<div id='elem'>
</div>
<button name='btn' id='btn'>Add</button>

An alternate way is to add to options list of select.
var select = document.getElementById('select');
var options = select.options;
options[options.length] = new Option("Option 1", "1");
options[options.length] = new Option("Option 2", "2");
<select id="select"></select>
HTMLSelectElement.add() is another possibility.
var sel = document.createElement("select");
var opt1 = document.createElement("option");
var opt2 = document.createElement("option");
opt1.value = "1";
opt1.text = "Option: Value 1";
opt2.value = "2";
opt2.text = "Option: Value 2";
sel.add(opt1, null);
sel.add(opt2, null);
document.body.appendChild(sel);

You have add option1 element to select element instead of container element.
Change line:
container.appendChild(option1);
To:
select.appendChild(option1);

Related

Get values of multiple select boxes in PHP and add them to an array

I have a button which allows an ingredient to be added to a database. When the button is clicked, the user can add the name, measurement and unit for the ingredient which is done using Javascript:
function addIngredient() {
var area = document.getElementById("addedIngredient");
var num = document.createElement("p");
numText = document.createTextNode(countIngredient + ". ");
num.appendChild(numText);
area.appendChild(num);
//Ingredient Name
var ingredientNameLabel = document.createElement("p");
ingredientNameLabelText = document.createTextNode("Name");
ingredientNameLabel.appendChild(ingredientNameLabelText);
area.appendChild(ingredientNameLabel);
countIngredient++;
var ingredientNameInput = document.createElement("INPUT");
ingredientNameInput.setAttribute("name", "ingredient_name[]");
ingredientNameInput.setAttribute("type", "text");
ingredientNameInput.setAttribute("class", "form-control");
ingredientNameInput.setAttribute("class", "ingName");
area.appendChild(ingredientNameInput);
//Ingredient Measure
var ingredientMeasureLabel = document.createElement("p");
ingredientMeasureLabelText = document.createTextNode("Measure");
ingredientMeasureLabel.appendChild(ingredientMeasureLabelText);
area.appendChild(ingredientMeasureLabel);
var ingredientMeasureInput = document.createElement("INPUT");
ingredientMeasureInput.setAttribute("name", "ingredient_measure[]");
ingredientMeasureInput.setAttribute("type", "text");
ingredientMeasureInput.setAttribute("class", "form-control");
ingredientMeasureInput.setAttribute("class", "ingMeasure");
area.appendChild(ingredientMeasureInput);
//Ingredient Unit
var ingredientUnitLabel = document.createElement("p");
ingredientUnitLabelText = document.createTextNode("Unit");
ingredientUnitLabel.appendChild(ingredientUnitLabelText);
area.appendChild(ingredientUnitLabel);
var select = document.createElement("SELECT");
select.setAttribute("name", "ingredient_unit[]");
area.appendChild(select);
var option = document.createElement("option");
option.setAttribute("value", "grams");
var value = document.createTextNode("g");
option.appendChild(value);
select.appendChild(option);
var option = document.createElement("option");
option.setAttribute("value", "milimeters");
var value = document.createTextNode("ml");
option.appendChild(value);
select.appendChild(option);
var option = document.createElement("option");
option.setAttribute("value", "kilograms");
var value = document.createTextNode("kg");
option.appendChild(value);
select.appendChild(option);
var option = document.createElement("option");
option.setAttribute("value", "litres");
var value = document.createTextNode("litre(s)");
option.appendChild(value);
select.appendChild(option);
var option = document.createElement("option");
option.setAttribute("value", "slice");
var value = document.createTextNode("slice");
option.appendChild(value);
select.appendChild(option);
var option = document.createElement("option");
option.setAttribute("value", "whole");
var value = document.createTextNode("whole");
option.appendChild(value);
select.appendChild(option);
var option = document.createElement("option");
option.setAttribute("value", "pinch");
var value = document.createTextNode("pinch");
option.appendChild(value);
select.appendChild(option);
}
I am looking to get the unit selected for every ingredient added.
Can anyone tell me how I could do this using PHP?
I am trying the following way:
$units = [];
foreach ($_POST["ingredient_unit[]"] as $key => $unit) {
array_push($units, $unit);
}
This does not work nor does it give an error.
Thanks.
Change the following code:
foreach ($_POST["ingredient_unit[]"] as $key => $unit) {
array_push($units, $unit);
}
And use this instead:
foreach ($_POST["ingredient_unit"] as $key => $unit) {
array_push($units, $unit);
}
And you should be getting a notice when you try to access $_POST["ingredient_unit[]"] since its not defined. If you are not seeing a notice, maybe the error_reporting level is too low. You could use something like the following to active all errors except the deprecated ones:
error_reporting(E_ALL &~ E_DEPRECATED);

Creating elements in javascript and appending them to the DOM

Here's a couple of small questions that I need help with...
I am trying to add multiple values to the select/option form but when I do that the javascript function ends up only adding the last one - here are the details for you to check out -
function addFields1(){
var container = document.getElementById("container1");
container.appendChild(document.createTextNode("Institution Name: "));
var name = document.createElement("input");
name.type = "text";
name.id = "name";
name.name = "certificate";
name.size = 25;
name.maxlength = 25;
container.appendChild(name);
var select = document.createElement("select"); //? how do I fix this up
var option = document.createElement("option");
option.value = "College";
option.innerHTML = "College";
option.value = "Community College";
option.innerHTML = "Community College";
option.value = "High School";
option.innerHTML = "High School";
option.value = "Private";
option.innerHTML = "Private";
option.value = "University";
option.innerHTML = "University";
select.appendChild(option);
container.appendChild(select);
}
When I call the function the only value that shows up is the very last one ("University") - the remaining values are not even on the menu when it drops down.
Another (similar) problem I am having is appending <td> and <input> elements and not being able to print any data when the eventhandler is called -
Here's the code for that below -
function aFields1(){
var container1 = document.getElementById("container");
container1.appendChild(document.createTextNode("Company: "));
var td = document.createElement("td");
var company = document.createElement("input");
company.type = "text";
company.id = "company";
company.name = "company";
company.size = 15;
company.maxlength = 15;
td.appendChild(company);
container1.appendChild(td);
container1.appendChild(company);
}
When i remove the td variable and td element, the information prints properly but if I don't do that it doesn't show up at all! Is there something wrong with what I did in this second function?
With the option elements you actually need to re-run the document.createElement('option') otherwise javascript retains a reference to the originally added option element and only updates it (rather than appending a new element). This allows us to re-use the option variable.
The issue with the <td> element is similar... this line container1.appendChild(company); moves the company input element outside of the td element and into the container1 element.
I have cleaned up and fixed both of your functions:
function addFields1() {
var container = document.getElementById("container1");
container.appendChild(document.createTextNode("Institution Name: "));
var name = document.createElement("input");
name.type = "text";
name.id = "name";
name.name = "certificate";
name.size = 25;
name.maxlenth = 25;
container.appendChild(name);
//create select element and the option variable
var select = document.createElement("select");
var option;
//before each option element we reassign the option variable to a new option element and append it to the select before moving to the next option item
option = document.createElement("option");
option.value = "College";
option.innerHTML = "College";
select.appendChild(option);
option = document.createElement("option");
option.value = "Community College";
option.innerHTML = "Community College";
select.appendChild(option);
option = document.createElement("option");
option.value = "High School";
option.innerHTML = "High School";
select.appendChild(option);
option = document.createElement("option");
option.value = "Private";
option.innerHTML = "Private";
select.appendChild(option);
option = document.createElement("option");
option.value = "University";
option.innerHTML = "University";
select.appendChild(option);
//finally, we append the completed select element
container.appendChild(select);
}
function aFields1() {
var container1 = document.getElementById("container");
container1.appendChild(document.createTextNode("Company: "));
var td = document.createElement("td");
var company = document.createElement("input");
company.type = "text";
company.id = "company";
company.name = "company";
company.size = 15;
company.maxlenth = 15;
//append the company input element to the td element
td.appendChild(company);
//append the td element to the container1 element
container1.appendChild(td);
}
JSFIDDLE DEMO

Auto populate option tag not working java script

I'm trying to populate number in select - option tag .But my script fills number at only one select tag
HTML :
<div class="form_row">
<label>Week Num</label>
<select class="form_select1" id="weeknum"></select>
<b class="bold1">&nbsp TO &nbsp</b>
<select class="form_select1" id="weeknum1"></select>
</div>
Javascript :
<script>
var select = document.getElementById("weeknum");
var select1 = document.getElementById("weeknum1");
for (var i = 52; i >= 1; i--) {
var option = document.createElement('option');
option.text = option.value = i;
select.add(option, 0);
select1.add(option, 0);
}
</script>
How do i achieve that ?
You can add an element only once to a parent node, so you need to create another option element:
var select = document.getElementById("weeknum");
var select1 = document.getElementById("weeknum1");
for (var i = 52; i >= 1; i--) {
var option = document.createElement('option');
option.text = option.value = i;
var option1 = document.createElement('option');
option1.text = option1.value = i;
select.add(option, 0);
select1.add(option1, 0);
}
Here's the JSFIddle.
Try this ...
var option1 = document.createElement('option');
option1.text = option1.value = i;
select.add(option1, 0);
var option2 = document.createElement('option');
option2.text = option2.value = i;
select1.add(option2, 0);
... creating a distinct "option" for each select list. The created elements cannot be reused.
You need to create a distinct option element for each list. A given DOM element can only be in one place in the DOM.
edit: I see it's already answered... well, my first answer :)
try this:
var select = document.getElementById("weeknum");
var select1 = document.getElementById("weeknum1");
for (var i = 52; i >= 1; i--) {
var option = document.createElement('option');
var option1 = document.createElement('option');
option.text = option.value = option1.text = option1.value = i;
select.add(option, 0);
select1.add(option1, 0);
}

how to select the option value in dynamic html table javascript. not getting updated option value from the database in the jsp page

element1 = document.createElement("select");
var option1 = document.createElement("option");
option1.value="warm";
option1.innerHTML="warm";
element1.appendChild(option1);
var option2 = document.createElement("option");
option2.value="hot";
option2.innerHTML="hot";
element1.appendChild(option2);
You can set the options selected property
var element1 = document.createElement("select");
var option1 = document.createElement("option");
option1.value = "warm";
option1.innerHTML = "warm";
element1.appendChild(option1);
var option2 = document.createElement("option");
option2.value = "hot";
option2.innerHTML = "hot";
option2.selected = true; // LIKE THIS
element1.appendChild(option2);
FIDDLE

how to populate drop down list using java script

How to populate drop down value in java script?
<script type="text/javascript">
var creatLimit = 5;
var fCount=0;
function addFileElement()
{
if(fCount <creatLimit )
{
/*var option = document.createElement("option");
option.value = '0'; option.innerHTML = ' -- '; select.appendChild(option);
option.value = '1'; option.innerHTML = 'item 1'; select.appendChild(option);
option.value = '2'; option.innerHTML = 'item 2'; select.appendChild(option);*/
var fObject = document.getElementById("agencySection");
//var addButton = document.createElement(label);
var addButton = document.createElement("select");
var agency = document.getElementById("agencyLabelSection");
var addButton2 = document.createElement("option");
//<label for="firstname">First name:</label>
//<label for="agencyLabelSection">agency:</label>
//<input type="text" name="agencyLabelSection" id="agencyLabelSection" />
addButton.type= "select";
addButton2.type= "option";
//for (var fObject in addButton) {
addButton.name="userRoleBeanList["+fCount+"]";
addButton.setAttribute("class", "normal");
addButton.style.width= "250px";
addButton.onkeydown = function(){
blur();
}; //}
//document.write("<p> Agency:</p>");
addButton2.name="userRoleBeanList["+fCount+"]";
addButton2.setAttribute("class", "normal");
addButton2.onkeydown = function(){
blur();
};
var o2 = document.createElement("br");
var o3 = document.createElement("br");
fObject.appendChild(addButton);
fObject.appendChild(o2);
fObject.appendChild(o3);
agency.appendChild(addButton2);
var fObject1 = document.getElementById("roleSection");
var addButton1 = document.createElement("select");
var role = document.getElementById("roleLabelSection");
var addButton3 = document.createElement("option");
addButton1.type= "select";
addButton3.type= "option";
addButton1.name="userRoleBeanList["+fCount+"]";
addButton1.setAttribute("class", "normal");
addButton1.style.width= "250px";
addButton1.onkeydown = function(){
blur();
};
var o4 = document.createElement("br");
var o5 = document.createElement("br");
fObject1.appendChild(addButton1);
fObject1.appendChild(o4);
fObject1.appendChild(o5);
role.appendChild(addButton3);
fCount++;
}
}
</script>
the same question was asked here and the answer is just
ddl.options[i] = theOption;
this code example show how to add variables to the drop down:
var ddl = document.getElementById( 'myDropdown' );
var theOption = new Option;
var x;
var i;
for(i = 0; i < 999; i++) {
x = i + 1;
theOption.text = x;
theOption.value = x;
ddl.options[i] = theOption;
}
if you'll edit your question so we'll know what's the drop down list name and needed values are then i can help you more

Categories

Resources