Tuesday, August 13, 2019

JavaScript drag-drop

https://flaviocopes.com/drag-and-drop/


https://flaviocopes.com/keyboard-events/


https://flaviocopes.com/form-events/






MORE TIPS>

TCC SUPER TUTORIAL:

http://mclaveau.com/grimoire/tcc.htm

http://mclaveau.com/grimoire/tcc2.htm


|[]|



:
  1. <section id="mysection">
  2. <div id=”mydiv” class = “myclass”>
  3. <h1 id=”mytitle”> A nice title </h1>
  4. <p id=”mytext”> Some intersting text </p>
  5. </div>
  6. <div id=”anotherdiv” class = “myclass”>
  7. <img scr="myimage.png" alt=""/>
  8. </div>
  9. </section>
The div has to child nodes: 1 and p.
It also have a parent node: section.
Let’s see how to work with this small HTML DOM…
1. How to find our div ?
  1. //By its id (retun an unique element)
  2. var myDiv = document.getElementById("mydiv")
  3. //By class name (return an array of elements)
  4. var myarray = document.getElementsByClassName("myclass")
  5. var myDiv = myarray[0]
  6.  
  7. //By tag name (return an array of elements)
  8. var myarray = document.getElementsByTagName("div")
  9. var myDiv = myarray[0]
2. How to find my div’s relatives ?
  1. // child nodes (array)
  2. myDiv.childNodes
  3.  
  4. // first child node
  5. myDiv.firstChild
  6.  
  7. // last child node
  8. myDiv.lastChild
  9.  
  10. // check is the div has childs (true of false)
  11. myDiv.hasChildNodes
  12.  
  13. // next node at the same level
  14. myDiv.nextSibling
  15.  
  16. // previous node at the same level
  17. myDiv.previousSibling
  18.  
  19. // parent node
  20. myDiv.parentNode
3. Change my div’s attributes
  1. myDiv.class = "anotherclass"
  2. myDiv.style.color = "red"
4. Create and insert an new element
Just clone our initial paragraph:
  1. // Clone an existing HTML element
  2. var myP = document.getElementsByTagName("p")[0]
  3. var myParagraph = myP.cloneNode()
Or create a brand new paragraph element:
  1. // Create an element node
  2. var myP = document.createElement("p")
  3.  
  4. // Create a text node
  5. var myText = document.createTextNode("Some new text")
  6.  
  7. // Combine the element and text node to create a new HTML element
  8. var myParagraph = myP.appendChild(myText)
Append the paragraph at the end of the div childs.
This will make the paragraph appear on the page.
  1. myDiv.appendChild(myParagraph)
5. If you made an error, let’s replace this element…
Of course it would be faster to replace the text inside but we are a little blunt so…
  1. var newP = document.createElement("p");
  2. var newText = document.createTextNode("This is new text");
  3. newP.appendChild(newText);
  4.  
  5. myDiv.replaceChild(newP, myParagraph);
6. Ok, this was fun, now remove this second P element…
This remove the parapher from the div:
  1. myDiv.removeChild(newP);
It is not mandatory to know the parent:
  1. newP.parentNode.removeChild(newP);
7. Or do we need it ? Let’s make an animation that add it and remove it after 5s.
Ok, this is just a terrible idea but who mind…
  1. setTimeout(myDiv.appendChild(newP), 5);
  2. setTimeout(myDiv.removeChild(newP), 10);

No comments:

Post a Comment

Beogradsko programiranje=