Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript and the DOM Making Changes to the DOM Get and Set Content with textContent and innerHTML

How would you update the Heading with the "Enter" key?

I tried the code below, and I couldn't get it to work. It also made the click event quit working. (And yes, I plan to make a function to update the headline.)

if (headlineInput === document.activeElement) {
  document.addEventListener('keypress', (e) => {
        if (e.key === 'Enter) {
          headline.textContent = headlineInput.value;
          headlineInput.value = '';  
         }
     }
  });
}

2 Answers

I was able to figure it out after a little more trial and error. Here is what worked for me:

const headline = document.getElementById('headline');
const updateHeadingBtn = document.getElementById('btn-main');
const headlineInput = document.getElementById('main');

updateHeadingBtn.addEventListener('click', () => {
   headline.textContent = headlineInput.value;
   headlineInput.value = '';
});

headlineInput.addEventListener('keypress', function (event) {
  if (event.key === 'Enter') {
    headline.textContent = headlineInput.value;
    headlineInput.value = '';
  }
});

Try changing document.addEventListener to headline.addEventListener and 'Enter to 'Enter'

Thanks for the suggestions! It still isn't working, unfortunately.