0

I am trying to add an <input> element using Javascript. However, the innerHTML is not showing up. Below is my code:

function addElements()
{
    container = document.getElementById("duration"); //a div container
    var input1 = document.createElement("input");
    var input2 = document.createElement("input");
    input1.type = "number";
    input1.min = "0";
    input1.max = "10";
    input1.required = true;
    input1.innerHTML = "years";
    input2.type = "number";
    input2.min = "0";   
    input2.max = "12";
    input2.required = true;
    input2.innerHTML = "months";
    container.appendChild(input1);
    container.appendChild(input2);
}

The result of this code only produces two number input fields without the innerHTML. Is there anything wrong with my code?

2
  • 5
    Inputs have no content. Commented Apr 10, 2015 at 19:20
  • can we give the input type after appeding a new element Commented Apr 10, 2015 at 19:22

2 Answers 2

3

Input elements don't have inner content you can set with innerHTML. Instead set value property:

input1.value = "years";

However, it seems that in your case you want to set placeholder:

input1.setAttribute("placeholder", "years");

or you can set corresponding property as well:

input1.placeholder = "years";
Sign up to request clarification or add additional context in comments.

6 Comments

But they are number inputs. The value can't be "years".
I think you're trying to do an HTML5 placeholder? set the placeholder attribute to "years"
I recommend always using .setAttribute
Thanks for the quick replies. However, I am not trying to create a placeholder.
@jl90, then you need to explain what you're trying to do because you defined something as allowing only 0-10 and provide an example of trying to set it to "years." Since an input tag has no innerHTML, I'm not sure what you're goal is unless you provide more detail.
|
2

I think what you try to achieve is [label][input] in this case you have to add 2 new more elements on page.

function addElements()
{
    container = document.getElementById("duration"); //a div container
    var label1 = document.createElement("label");
    var input1 = document.createElement("input");
    
    var label2 = document.createElement("label");
    var input2 = document.createElement("input");
    
    input1.type = "number";
    input1.min = "0";
    input1.max = "10";
    input1.required = true;
  
    label1.innerHTML = "years";
  
    input2.type = "number";
    input2.min = "0";   
    input2.max = "12";
    input2.required = true;
  
    label2.innerHTML = "months";
    
    container.appendChild(label1);
    container.appendChild(input1);
    container.appendChild(label2);
    container.appendChild(input2);
}

addElements();
<div id="duration" />

Comments

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.