Does element.appendChild(node) applies properties of parent node to child node?

Meghana M

I am new to HTML and JS. Need to create dynamic expand-collapse list. var parentId = document.getElementById("ABCD") parentId.setAttribute("data-toggle","collapse") parentId.setAttribute("data-target","#collapse1") var tag = document.createElement("ul"); tag.setAttribute("id","collapse1") tag.appendChild(document.createTextNode("PQR")) parentId.appendChild(tag)

Trying for list as- ABCD PQR

So in this case, when i am clicking on ABCD, PQR gets expanded/collapsed. But the problem is on clicking on PQR, it gets collapsed again. So does the properties of parent gets applied to child node also?

dave.mcalpine

it's not that it gets properties of it's parent, this has to do with how events handled, specifically event bubbling. When you click a child element, a click event if fired for all parent elements of what you clicked on

to cancel the event from bubbling when you click the appended elements you need to event.stopPropagation() inside of a click handler for the new elements

after you append them do the following

// tag.appendchild code goes here, then do the following

document.querySelector("#collapse1").onclick=function(event){
    event.stopPropagation();
}

also, i should mention all this would be 10 times easier with jQuery

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

remove parent node depending on the child element values

From Dev

Get the parent node on the basis of single child element with specific attribute

From Dev

HTML issue: Is it okay if I close a parent element and then a child node?

From Java

XPath: Get parent node from child node

From Dev

Adding child node to parent node of JTree

From Dev

Remove parent node if child node is empty

From Dev

Parent Node or Child Node in Blackberry Tree Field?

From Dev

Adding child node to parent node of JTree

From Dev

XSL parent node data in child node

From Dev

Comparing a parent and a child node for similarity

From Dev

Unchecked parent if child node is unchecked

From Dev

Knockout binding parent and child node

From Dev

parent node losing its child

From Dev

javascript check if child node is element or text node

From Dev

JavaScript - Appending element node to text node parent

From Dev

VBA get parent node of element

From Dev

Remove node function on parent element

From Dev

Remove node function on parent element

From Dev

Copying a Child Node into its Parent's Sibling's Child Node

From Dev

Add common properties of the child objects and append it with Parent Key (Node.js)

From Dev

How to find a child element with a specific value and delete it's immediate parent node?

From Dev

How do I get the dom node of a child element in a component without adding logic to its parent/children?

From Dev

Uncaught HierarchyRequestError: Failed to execute 'insertBefore' on 'Node': The new child element contains the parent

From Dev

How to find a child element with a specific value and delete it's immediate parent node?

From Dev

How do I get the dom node of a child element in a component without adding logic to its parent/children?

From Dev

SQL2 - Get child node properties

From Dev

How to rotate parent SKSpriteNode only and not the child node

From Dev

Child not moving when parent node moves?

From Dev

Saving a child when deleting a parent node

Related Related

  1. 1

    remove parent node depending on the child element values

  2. 2

    Get the parent node on the basis of single child element with specific attribute

  3. 3

    HTML issue: Is it okay if I close a parent element and then a child node?

  4. 4

    XPath: Get parent node from child node

  5. 5

    Adding child node to parent node of JTree

  6. 6

    Remove parent node if child node is empty

  7. 7

    Parent Node or Child Node in Blackberry Tree Field?

  8. 8

    Adding child node to parent node of JTree

  9. 9

    XSL parent node data in child node

  10. 10

    Comparing a parent and a child node for similarity

  11. 11

    Unchecked parent if child node is unchecked

  12. 12

    Knockout binding parent and child node

  13. 13

    parent node losing its child

  14. 14

    javascript check if child node is element or text node

  15. 15

    JavaScript - Appending element node to text node parent

  16. 16

    VBA get parent node of element

  17. 17

    Remove node function on parent element

  18. 18

    Remove node function on parent element

  19. 19

    Copying a Child Node into its Parent's Sibling's Child Node

  20. 20

    Add common properties of the child objects and append it with Parent Key (Node.js)

  21. 21

    How to find a child element with a specific value and delete it's immediate parent node?

  22. 22

    How do I get the dom node of a child element in a component without adding logic to its parent/children?

  23. 23

    Uncaught HierarchyRequestError: Failed to execute 'insertBefore' on 'Node': The new child element contains the parent

  24. 24

    How to find a child element with a specific value and delete it's immediate parent node?

  25. 25

    How do I get the dom node of a child element in a component without adding logic to its parent/children?

  26. 26

    SQL2 - Get child node properties

  27. 27

    How to rotate parent SKSpriteNode only and not the child node

  28. 28

    Child not moving when parent node moves?

  29. 29

    Saving a child when deleting a parent node

HotTag

Archive