0

I use JavaScript to create input text fields. And everything works fine, except attribute value. It simply doesn't generate and I don't know why?

Here's my code:

k=1;
function addtxt() {
    var tip = document.createElement("input");
    tip.type = "text";
    tip.name = "tip[" + k + "]";
    tip.value = "Question1";
    var div = document.createElement("div");
    div.innerHTML = "Question: " + tip.outerHTML + "<br />";
    document.getElementById("mydiv").appendChild(div);
k++
}

This is what I got:

<input type="text" name="tip[1]">
3
  • What is the problem i don't understood what you are trying tosay Commented Mar 25, 2014 at 11:04
  • There is no attribute "value". I defined it to be in this example "Question 1" and it simply doesn't generate. It should be something like <input type="text" name="tip[1]" value="Question1"> Commented Mar 25, 2014 at 11:06
  • provide jsfiddle for the same Commented Mar 25, 2014 at 11:06

1 Answer 1

4

may be try using setAttribute(), like, change:

tip.value = "Question1";

to

tip.setAttribute('value', "Question1");
Sign up to request clarification or add additional context in comments.

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.