DEV Community

Prarthana
Prarthana

Posted on

JavaScript DOM manipulation and EventHandling

It's going to be pretty lengthy vlog you can directly hover and read one particular command or enjoy extensive reading β€οΈπŸ’–

Essential JavaScript DOM manipulation commands:

  1. document.getElementById(): Selects an element by its ID.
   const element = document.getElementById('myElement');
Enter fullscreen mode Exit fullscreen mode
  1. document.querySelector(): Selects the first element that matches a CSS selector.
   const element = document.querySelector('.myClass');
Enter fullscreen mode Exit fullscreen mode
  1. document.querySelectorAll(): Selects all elements that match a CSS selector.
   const elements = document.querySelectorAll('.myClass');
Enter fullscreen mode Exit fullscreen mode
  1. document.createElement(): Creates a new HTML element.
   const newElement = document.createElement('div');
Enter fullscreen mode Exit fullscreen mode
  1. parentElement.appendChild(): Appends a child element to a parent element.
   parentElement.appendChild(newElement);
Enter fullscreen mode Exit fullscreen mode
  1. parentElement.insertBefore(): Inserts a new element before an existing element.
   parentElement.insertBefore(newElement, existingElement);
Enter fullscreen mode Exit fullscreen mode
  1. element.innerHTML: Gets or sets the HTML content of an element.
   element.innerHTML = '<p>New Content</p>';
Enter fullscreen mode Exit fullscreen mode
  1. element.textContent: Gets or sets the text content of an element.
   element.textContent = 'New Text';
Enter fullscreen mode Exit fullscreen mode
  1. element.setAttribute(): Sets an attribute on an element.
   element.setAttribute('class', 'newClass');
Enter fullscreen mode Exit fullscreen mode
  1. element.removeAttribute(): Removes an attribute from an element.

    element.removeAttribute('class');
    
  2. element.style.property: Sets the inline style of an element.

    element.style.color = 'red';
    
  3. element.classList.add(): Adds a class to an element.

    element.classList.add('newClass');
    
  4. element.classList.remove(): Removes a class from an element.

    element.classList.remove('oldClass');
    
  5. element.classList.toggle(): Toggles a class on an element.

    element.classList.toggle('activeClass');
    
  6. element.addEventListener(): Adds an event listener to an element.

    element.addEventListener('click', function() {
      alert('Element clicked!');
    });
    
  7. element.removeEventListener(): Removes an event listener from an element.

    element.removeEventListener('click', clickHandler);
    
  8. element.appendChild(): Appends a child node to an element.

    element.appendChild(childNode);
    
  9. element.removeChild(): Removes a child node from an element.

    element.removeChild(childNode);
    
  10. element.replaceChild(): Replaces a child node with another node.

    element.replaceChild(newChild, oldChild);
    
  11. element.cloneNode(): Clones an element.

    const clonedElement = element.cloneNode(true);
    
  12. element.contains(): Checks if an element contains a specific node.

    const containsNode = element.contains(childNode);
    
  13. element.parentElement: Returns the parent element of an element.

    const parent = element.parentElement;
    
  14. element.children: Returns a collection of child elements of an element.

    const children = element.children;
    
  15. element.firstElementChild: Returns the first child element of an element.

    const firstChild = element.firstElementChild;
    
  16. element.lastElementChild: Returns the last child element of an element.

    const lastChild = element.lastElementChild;
    

These commands cover a wide range of DOM manipulation tasks and will be very useful in your web development projects. Happy coding! πŸš€πŸ’»

EventHandling

Here are some key event handling commands in JavaScript:

  1. element.addEventListener(): Adds an event listener to an element.
   element.addEventListener('click', function() {
     alert('Element clicked!');
   });
Enter fullscreen mode Exit fullscreen mode
  1. element.removeEventListener(): Removes an event listener from an element.
   element.removeEventListener('click', clickHandler);
Enter fullscreen mode Exit fullscreen mode
  1. element.onclick: Sets an event handler for the click event.
   element.onclick = function() {
     alert('Element clicked!');
   };
Enter fullscreen mode Exit fullscreen mode
  1. element.onsubmit: Sets an event handler for the submit event.
   form.onsubmit = function(event) {
     event.preventDefault();
     alert('Form submitted!');
   };
Enter fullscreen mode Exit fullscreen mode
  1. element.onmouseover: Sets an event handler for the mouseover event.
   element.onmouseover = function() {
     element.style.color = 'blue';
   };
Enter fullscreen mode Exit fullscreen mode
  1. element.onmouseout: Sets an event handler for the mouseout event.
   element.onmouseout = function() {
     element.style.color = 'black';
   };
Enter fullscreen mode Exit fullscreen mode
  1. element.onkeydown: Sets an event handler for the keydown event.
   element.onkeydown = function(event) {
     console.log('Key pressed:', event.key);
   };
Enter fullscreen mode Exit fullscreen mode
  1. element.onkeyup: Sets an event handler for the keyup event.
   element.onkeyup = function(event) {
     console.log('Key released:', event.key);
   };
Enter fullscreen mode Exit fullscreen mode
  1. element.onchange: Sets an event handler for the change event (useful for input elements).
   element.onchange = function() {
     console.log('Value changed:', element.value);
   };
Enter fullscreen mode Exit fullscreen mode
  1. element.oninput: Sets an event handler for the input event (useful for real-time input tracking).

    element.oninput = function() {
      console.log('Input value:', element.value);
    };
    
  2. element.onfocus: Sets an event handler for the focus event.

    element.onfocus = function() {
      element.style.backgroundColor = 'lightyellow';
    };
    
  3. element.onblur: Sets an event handler for the blur event.

    element.onblur = function() {
      element.style.backgroundColor = 'white';
    };
    
  4. element.ondragstart: Sets an event handler for the dragstart event.

    element.ondragstart = function(event) {
      console.log('Drag started:', event);
    };
    
  5. element.ondrop: Sets an event handler for the drop event.

    element.ondrop = function(event) {
      event.preventDefault();
      console.log('Element dropped:', event);
    };
    
  6. element.ondragover: Sets an event handler for the dragover event.

    element.ondragover = function(event) {
      event.preventDefault();
    };
    
  7. element.ondblclick: Sets an event handler for the double-click event.

    element.ondblclick = function() {
      alert('Element double-clicked!');
    };
    
  8. element.oncontextmenu: Sets an event handler for the context menu event (right-click).

    element.oncontextmenu = function(event) {
      event.preventDefault();
      alert('Right-click menu opened!');
    };
    
  9. element.onresize: Sets an event handler for the resize event (useful for the window object).

    window.onresize = function() {
      console.log('Window resized to', window.innerWidth, 'x', window.innerHeight);
    };
    
  10. element.onselect: Sets an event handler for the select event (useful for text input elements).

    element.onselect = function() {
      console.log('Text selected:', element.value.substring(element.selectionStart, element.selectionEnd));
    };
    
  11. element.ontouchstart: Sets an event handler for the touchstart event (useful for touch devices).

    element.ontouchstart = function(event) {
      console.log('Touch started:', event.touches);
    };
    
  12. element.ontouchend: Sets an event handler for the touchend event (useful for touch devices).

    element.ontouchend = function(event) {
      console.log('Touch ended:', event.changedTouches);
    };
    
  13. element.onscroll: Sets an event handler for the scroll event.

    window.onscroll = function() {
      console.log('Page scrolled:', window.scrollY);
    };
    
  14. element.onwheel: Sets an event handler for the wheel event (mouse wheel scroll).

    element.onwheel = function(event) {
      console.log('Mouse wheel scrolled:', event.deltaY);
    };
    
  15. element.onkeypress: Sets an event handler for the keypress event.

    element.onkeypress = function(event) {
      console.log('Key pressed:', event.key);
    };
    
  16. element.oncopy: Sets an event handler for the copy event.

    element.oncopy = function(event) {
      console.log('Content copied:', event);
    };
    

These commands should help you handle various events in your JavaScript projects, making your web applications more interactive and responsive to user actions. Happy coding! πŸš€πŸ’»

Reached till here, then you are the champion !!

Top comments (0)