Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
How to change innerHTML or innerText of element via attribute in real-time

I want to change the innerHTML of .container in real-time via setAttribute('data-content', 'value changed');. Currently, I am appending innerHTML via .container.innerHTML = .container.getAttribute('data-content');

const container = document.querySelector('.container');
container.innerHTML = container.getAttribute('data-content');

const btn = document.querySelector('.btn');

// before clicking on button
console.log(container.getAttribute('data-content'));

btn.addEventListener('click', (e) => {
  container.setAttribute('data-content', 'Value Changed!!');
  console.log(container.getAttribute('data-content'));
});
* {
  margin: 0;
  padding: 0;
  outline: 0;
  box-sizing: border-box;
}

body {
  height: 100vh;
  display: flex;
  padding-top: 1rem;
  align-items: flex-start;
  justify-content: center;
}

.btn {
  cursor: pointer;
  margin-left: 1rem;
  padding: 0.25rem 0.5rem;
}
<div class="container" data-content="Hello, World!"></div>
<button class="btn">Change Attribute Value</button>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It is exactly as folks in comments said - You could create this binding with using some kind of "JS framework", where you would define binding between the attribute and the "value HTML/text value of <div> tag.

But in the plain JS/html, you have to set this value manually.

const container = document.querySelector('.container');
container.innerHTML = container.getAttribute('data-content');

const btn = document.querySelector('.btn');

// before clicking on button
console.log(container.getAttribute('data-content'));

btn.addEventListener('click', (e) => {
  container.setAttribute('data-content', 'Value Changed!!');
  console.log(container.getAttribute('data-content'));


  // Added line
  container.innerHTML = container.getAttribute('data-content');
});
* {
  margin: 0;
  padding: 0;
  outline: 0;
  box-sizing: border-box;
}

body {
  height: 100vh;
  display: grid;
  place-items: center;
}

.btn {
  cursor: pointer;
  padding: 0.25rem 0.5rem;
}
<div class="container" data-content="Hello, World!"></div>
<button class="btn">Change Attribute Value</button>

about 4 years ago · Juan Pablo Isaza Report

0

You can use attr function in CSS for pseudo element

const container = document.querySelector('.container');

const btn = document.querySelector('.btn');

// before clicking on button
console.log(container.getAttribute('data-content'));

btn.addEventListener('click', (e) => {
  container.setAttribute('data-content', 'Value Changed!!');
  console.log(container.getAttribute('data-content'));
});
* {
  margin: 0;
  padding: 0;
  outline: 0;
  box-sizing: border-box;
}

body {
  height: 100vh;
  display: grid;
  place-items: center;
}

.btn {
  cursor: pointer;
  padding: 0.25rem 0.5rem;
}
.container::before{
  content: attr(data-content);
}
<div class="container" data-content="Hello, World!"></div>
<button class="btn">Change Attribute Value</button>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!