0

I have a html page containing two textboxes with id name and mobile.

I've created a list and inserted the values into it.But its not taking the name value, its only taking the mobile textbox value. The other part is shown as undefined.

This is the code.

var li = document.createElement("li");
li.textContent = name.value +" , "+mobile.value;
list.appendChild(li);

What is the error ?

2
  • I would assume the issue is somewhere else: Show us how you are defining name? Commented Dec 27, 2013 at 17:56
  • <input type="text" id="name" required="required"> this is how i defined the name textbox.i dont think the problem is with defining...its showing undefined only when i assign it to li element. Commented Dec 30, 2013 at 10:21

1 Answer 1

1

"name" is a poor name for a field since if you do not qualify is like document.getElementsByName("name")[0] it could be window.name -

If the field has id="name" then use document.getElementById("name") instead since most browsers do not copy the ID attribute to the window scope

Thus

var li = document.createElement("li");
li.textContent = document.getElementById("name").value +" , "+
  document.getElementById("mobile").value; 
list.appendChild(li);

I also personally prefer li.innerHTML since it works in all browsers

Lastly remember that IDs must be unique

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.