Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to directly post tags into html body but a new problem is that i found error massages of undefined values, check my code suggest me what to do ?

What I have tried:

JavaScript
function body(el,at,txt)
{
el = document.createElement(el);
p.setAttribute(at);
t = document.createTextNode(txt);
el.appendChild(t);
}

body("p", "id='demo'", "i m paragraph");

/* this code doesnt work why */
Posted
Updated 29-Jul-16 3:07am
v2

1 solution

try this

HTML
<script>
       function body(el, at, txt) {
           var elem = document.createElement(el);
           var keyvalue = at.split('='); // split to get key and value
           elem.setAttribute(keyvalue[0], keyvalue[1]); // setAttribute accepts 2 param (key and value )
           t = document.createTextNode(txt);
           elem.appendChild(t);
           document.body.appendChild(elem); // append the element to body

       }

       body("p", "id=demo", "i m paragraph"); // remove the single quotes
   </script>


demo : JSFiddle[^]
 
Share this answer
 
v3
Comments
prasad sawant 29-Jul-16 14:52pm    
wow nice, thanks for attribute spliting and document.body syntax
Karthik_Mahalingam 29-Jul-16 15:25pm    
cool

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900