DEV Community

Cover image for Manipulating Elements
_Khojiakbar_
_Khojiakbar_

Posted on

Manipulating Elements

Changing Content

  • innerHTML: Gets or sets the HTML content inside an element.
element.innerHTML = '<p>New Content</p>';
Enter fullscreen mode Exit fullscreen mode
  • textContent: Gets or sets the text content of an element.
element.textContent = 'New Text';
Enter fullscreen mode Exit fullscreen mode
  • innerText: Similar to textContent but takes into account CSS styling.
element.innerText = 'New Text';
Enter fullscreen mode Exit fullscreen mode

Changing Attributes

  • getAttribute(): Gets the value of an attribute on the specified element.
const value = element.getAttribute('src');
Enter fullscreen mode Exit fullscreen mode
  • setAttribute(): Sets the value of an attribute on the specified element.
element.setAttribute('src', 'newImage.jpg');
Enter fullscreen mode Exit fullscreen mode
  • removeAttribute(): Removes an attribute from the specified element.
element.removeAttribute('src');
Enter fullscreen mode Exit fullscreen mode

Changing Styles

  • Using the style Property: Directly manipulate an element's inline styles.
element.style.color = 'red';
element.style.fontSize = '20px';
Enter fullscreen mode Exit fullscreen mode
  • Using classList Methods:

  • add: Adds a class to an element.

element.classList.add('newClass');
Enter fullscreen mode Exit fullscreen mode
  • remove: Removes a class from an element.
element.classList.remove('oldClass');
Enter fullscreen mode Exit fullscreen mode
  • toggle: Toggles a class on an element.
element.classList.toggle('activeClass');
Enter fullscreen mode Exit fullscreen mode
  • contains: Checks if an element contains a specific class.
element.classList.contains('someClass');
Enter fullscreen mode Exit fullscreen mode

Creating and Inserting Elements

  • createElement(): Creates a new element.
const newElement = document.createElement('div');
Enter fullscreen mode Exit fullscreen mode
  • appendChild(): Appends a child element to a parent element.
parentElement.appendChild(newElement);
Enter fullscreen mode Exit fullscreen mode
  • insertBefore(): Inserts an element before a specified child of a parent element.
parentElement.insertBefore(newElement, referenceElement);
Enter fullscreen mode Exit fullscreen mode
  • insertAdjacentHTML(): Inserts HTML text into a specified position.
element.insertAdjacentHTML('beforebegin', '<p>Before</p>');
element.insertAdjacentHTML('afterbegin', '<p>Start</p>');
element.insertAdjacentHTML('beforeend', '<p>End</p>');
element.insertAdjacentHTML('afterend', '<p>After</p>');
Enter fullscreen mode Exit fullscreen mode
  • append() and prepend(): Inserts nodes or text at the end or beginning of an element.
parentElement.append(newElement, 'Some text');
parentElement.prepend(newElement, 'Some text');
Enter fullscreen mode Exit fullscreen mode

Removing Elements

  • removeChild(): Removes a child element from a parent element.
parentElement.removeChild(childElement);
Enter fullscreen mode Exit fullscreen mode
  • remove(): Removes the specified element from the DOM.
element.remove();
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

typescript

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!