0

I have created an object with a list of attributes to add to html tag. I wanted to add these to a created element. This is the code

var up = {class:"upt", name:"positive_use",type:"text"};

createtextarea = document.createElement("TEXTAREA");

How to add these objects to the element. And how do you add more than one dynamically.

1
  • 1
    You add them one-by one using setAttribute? Commented Jul 4, 2019 at 3:53

2 Answers 2

0

You could run a foreach loop over the object keys of up and then use the setAttribute function.

Object.keys(up).forEach((k) => createtextarea.setAttribute(k, up[k])) // k would be the "class","name","type") in every iteration.
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah thank you. I redid the same with a function to create and add this
0

You can use setAttribute like this

var up={class:"upt", name:"positive_use",type:"text"};

let createtextarea = document.createElement('TEXTAREA');

createtextarea.setAttribute('class', up.class);
createtextarea.innerHTML = "TEST TEXT AREA";
createtextarea.setAttribute('name', up.name);
createtextarea.setAttribute('type', up.type);

document.body.appendChild(createtextarea);

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.