0

function add() {
  var e_type = document.createElement("p");

  var p_type = document.getElementById("self");

  var text = document.createTextNode("I am a slow walker, but I never walk back.");

  e_type.appendChild(text);

  p_type.appendChild(e_type);

  var pattribute = document.createAttribute("id");

  pattribute.value = "test";

  element.setAttributeNode(pattribute);
}
#test {
  color: blue;
  font-style: bold;
}
<div id="self">

  <p>Hello, Thought of the Day</p>

</div>

<button onclick="add()">Click me</button>

:- Added element text color is not changing by the use of createAttribute but rest of the tag are changing their color which are using this ID(test).

5
  • whts that element.setAttributeNode(pattribute); element here ? Commented Jul 19, 2016 at 10:09
  • what @gayathri said. Also you can use setAttribute('name', 'value') instead. Commented Jul 19, 2016 at 10:10
  • createElement of type "p" :- var e_type = document.createElement("p"); and its textElement :- var text = document.createTextNode("I am a slow walker, but I never walk back."); Commented Jul 19, 2016 at 10:10
  • 2
    element is not defined Commented Jul 19, 2016 at 10:11
  • I've updated your code to be a runnable snippet in the browser - note how it displays the error when you run the code. If you open your browser dev tools (usually F12), you will similarly be able to see these errors in your own pages while you're developing. Commented Jul 19, 2016 at 10:15

2 Answers 2

1
element.setAttributeNode(pattribute); 

is wrong

if you want to style the new p make it

e_type.setAttributeNode(pattribute);

if you need to style them both

p_type.setAttributeNode(pattribute);
Sign up to request clarification or add additional context in comments.

1 Comment

** Thanks @eltonkamami **
1

refere this becuase your question element is not defined https://plnkr.co/edit/FmaXvsWdVFT7mSB3whxs?p=preview

i changed to

 e_type.setAttributeNode(pattribute);

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.