The parent element knows its children. This might seem shocking to us humans, but in js a child knows its parent and a parent knows its child on creation. If I were to liken this to rails talk; a child belongs to a parent, a parent has many children. That is how I think about it, don’t quote me on that though. So parents and children know each other which means?
I modeled some code after what I found here. The result is this.
Like the source code since I barely changed it, if you were to push the button it would tell you the parent of Coffee. To do that it used the id of an element which happened to point to coffee, and then asked that element to name its parent. As mentioned earlier the parent and child know each other which is why we can do this.
Because of this relationship I want to think it will act like an active record relationship. I know that from the parent you can add children, update children, delete children(not winning any parenting awards for this.). I am sure that I will become more familiar with the parent and child element. As of right now being able to relate it to the active record belongs to, gives me a little confidence to push on. I also know for a fact this can’t happen in js.
Children can not remove their parents, but the parents can remove their children and they do it with the greatest of ease. A child can not remove their parent because that would effectively remove themselves, a child needs a parent not the other way around.
In this gif I ask for the a parent element with the ‘do it’ button. It tells me the parent of the first item. When the ‘bad parenting’ button is pressed, It removes all the children of that parent we looked at with the “do it” button. Since the child no longer exists the “do it” button now says the children are gone.
Maybe the ‘bad parenting’ button should say “the children are gone” and the ‘do it’ button should say “what child?” Next time, in the mean time you should have seen some beginner js and, how referencing Rails has helped me to not get lost in this new world. Something familiar will always help put you on the right path.