2

Having an issue learning JavaScript in a college course. This is my last project and the problem I am having with the HTML code is creating an input field for a label without an id, it has a "for" element though. It cannot be created with JQuery or JSON.

So the HTML looks something like this: <"label for="car">What car do you have? <"span class="required"><"/span><"/label">

So I was thinking I could create the input text field and add it to the DOM:

// Create the input field
var firstCar = document.createElement("input");
// Create the type node and store what the field will be text
var newType = document.createTypeNode("text");
// Create a new ID of car and attach it to the input
var newID = document.createIdNode("car")
/* put the created Element within the HTML with ["this determines which label it will be under"]:*/
var position = document.getElementByTagName("label")[1];
// Insert the element into the position
position.appendChild(firstCar);

I ran this and it says that TypeNode is not a function in my browser.

3
  • FYI - When an input element does not specify the type attribute, it defaults to <input type='text'>. Commented Apr 30, 2017 at 21:02
  • Our webpage changed and this is no longer a requirement. However, if someone would like to answer this still I would appreciate it to learn more about DOM manipulation. Thank you Marcus. Commented Apr 30, 2017 at 21:05
  • It's getElementsByTagName, note the plural Elements. Commented Apr 30, 2017 at 22:06

1 Answer 1

3

That's not how you set the type and id. Try this

var firstCar = document.createElement("input");

firstCar.type = "text"; // set the type

firstCar.id = "car";    // set the ID

var position = document.getElementsByTagName("label")[1]; // it should be getElements... not getElement...

position.appendChild(firstCar);
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.