# P5.dom.js 'createElement' function not able to set properties?

**URL:** <https://discourse.processing.org/t/p5-dom-js-createelement-function-not-able-to-set-properties/4196>\
**Category:** Coding Questions\
**Created:** [October 6, 2018, 5:53pm UTC](https://discourse.processing.org/t/p5-dom-js-createelement-function-not-able-to-set-properties/4196 "2018-10-06T17:53:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![repgainer3](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@repgainer3](https://discourse.processing.org/u/repgainer3)\
**Post date:** [October 6, 2018, 5:53pm UTC](https://discourse.processing.org/t/p5-dom-js-createelement-function-not-able-to-set-properties/4196/1 "2018-10-06T17:53:06Z")

</div>

Hi, I’m building a forum site from scratch and I’m using the p5.dom.js library to speed up the process.  
I’m attempting to create a TR tag and three TD tags that go inside the TR, after that I want the TR to go inside of my TBODY of choice (the one with the id of ‘forums’); However this is not the case, the row and columns are created just fine, but I’m not able to set their parentElement or any attribute for that matter. What am I doing wrong?

```
var allPosts;
function setup() {
    noCanvas();
    allPosts = loadStrings("./allPosts.txt", ready);
}
function ready() {
    console.log(allPosts);
    var post = document.getElementById("forums");
    var curPost = createElement("tr", "");
    var username = createElement("td", "Test Username");
    var subject = createElement("td", "Test Subject");
    var content = createElement("td", "This is text content.");
    curPost.elt.parentElement = post;
    username.elt.parentElement = curPost;
    subject.elt.parentElement = curPost;
    content.elt.parentElement = curPost;
}
```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 6, 2018, 6:35pm UTC](https://discourse.processing.org/t/p5-dom-js-createelement-function-not-able-to-set-properties/4196/2 "2018-10-06T18:35:06Z")

</div>

> [@repgainer3](#):
>
> `var post = document.getElementById("forums");`

Maybe use p5::**select()** instead of **getElementById()**: 🤔

> **[reference | p5.js](https://p5js.org/reference/#/p5/select)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

`const post = select('forums');`  
`const post = select('#forums');`

Doing so, you can now invoke method p5.Element::**child()**, passing _post_ as its argument: 🕶

> **[reference | p5.js](https://p5js.org/reference/#/p5.Element/child)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

Some sketch using both p5.Element::**child()** & p5.Element::**parent()** methods: 😇

> <https://gist.github.com/GoSubRoutine/bef70ffdae5e1a052ade9c6b15f737cc#file-sketch-js>
>
> There are more than three files. show original

---

<div class="post-metadata">

**Author:** ![repgainer3](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@repgainer3](https://discourse.processing.org/u/repgainer3)\
**Post date:** [October 6, 2018, 6:53pm UTC](https://discourse.processing.org/t/p5-dom-js-createelement-function-not-able-to-set-properties/4196/3 "2018-10-06T18:53:00Z")

</div>

Thank you for the help. I really think you’re on to something, but currently your solution gives me this error

```
site.html:23 Uncaught (in promise) TypeError: Cannot read property 'child' of null
at ready (site.html:23)
at p5.js:56385
at p5.js:57041

```

I know for certain the “forums” is an id of a TBODY element.  
So what’s going on?

Here’s the full page code:

```
<!DOCTYPE HTML>
<HTML lang="en">
<head>
	<title>Cameron Schoenecker: Forum Site</title>
	<meta charset="utf-8" />
	<link rel="stylesheet" href="../styles.css">
	<script src="p5.js"></script>
	<script src="p5.dom.js"></script>
	<script>
		var allPosts;
		function setup() {
			noCanvas();
			allPosts = loadStrings("./allPosts.txt", ready);
		}
		function ready() {
			console.log(allPosts);
			const post = select("forums");
			var curPost = createElement("tr", "");
			var username = createElement("td", "Test Username");
			var subject = createElement("td", "Test Subject");
			var content = createElement("td", "This is text content.");

			post.child(curPost);
			//username.elt.parentElement = curPost;
			//subject.elt.parentElement = curPost;
			//content.elt.parentElement = curPost;
		}
	</script>
</head>
<body>
	<div class="main">
		<div class="post"><a href="post.html"><h2 class="post">Post Thread</h2></a></div>
		<h2 style="margin-left: 5%"><span style="text-decoration: underline">Main Page:</span> <span style="font-style: italic; font-size: 15px">you can put any topic here</span></h2>
		<table class="forum">
			<tbody id="forums">
				<tr>
					<th><span style="font-style: italic">User Name</span></th>
					<th><span style="font-style: italic">Subject</span></th>
					<th><span style="font-style: italic">Content</span></th>
				</tr>
				<tr class="forum">
					<td>Anonymous</td>
					<td>test</td>
					<td>example text...</td>
				</tr>
			</tbody>
		</table>
		<hr></hr>
	</div>
	<div class="header">
  <p style="position: absolute; margin-left: 10px"><a href="../home.html">Back Home</a></p>
		<h1 style="text-align: center; margin-bottom: 0px">Forum Page</h1>
		<h1 style="text-align: center; margin-top: 0px">Post Anything Here</h1>
	</div>
	<div class="r">
		<img style="width: 100%" src="../cmd.gif" alt="Command Prompt" />
	</div>
	<div class="l">
		<img style="width: 100%" src="../cmd.gif" alt="Command Prompt" />
	</div>
</body>
</HTML>
```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 6, 2018, 7:03pm UTC](https://discourse.processing.org/t/p5-dom-js-createelement-function-not-able-to-set-properties/4196/4 "2018-10-06T19:03:59Z")

</div>

I’ve made a mistake at `const post = select('forums');`! 🥴  
Forgot to prefix it w/ `#`, so it searches for the _id_ attribute: `const post = select('#forums');` 🤦‍♂️

[https://p5js.org/reference/#/p5/select](https://p5js.org/reference/#/p5/select)

---

<div class="post-metadata">

**Author:** ![repgainer3](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@repgainer3](https://discourse.processing.org/u/repgainer3)\
**Post date:** [October 6, 2018, 7:04pm UTC](https://discourse.processing.org/t/p5-dom-js-createelement-function-not-able-to-set-properties/4196/5 "2018-10-06T19:04:57Z")

</div>

Thank you a million times over @GoToLoop! This works perfectly!
