Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript and the DOM (Retiring) Responding to User Interaction Listening for Events with addEventListener()

Jiten Mehta
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jiten Mehta
Front End Web Development Techdegree Graduate 21,118 Points

Different way of changing list items to uppercase

Hi guys,

to change the text on hover to upperCase for the list items, Guil used the textContent property and then the toUpper/Lower case methods.

for (let i = 0; i < listItems.length; i += 1) {
  listItems[i].addEventListener('mouseover', () => {
    listItems[i].textContent = listItems[i].textContent.toUpperCase();
  });
  listItems[i].addEventListener('mouseout', () => {
    listItems[i].textContent = listItems[i].textContent.toLowerCase();
  });
}

I used the style property and then the textTransform property. This worked for me, but i wanted to ask if one method is better then the other?

my Code below. Thanks!

for (let i=0; i < listItems.length; i++){
listItems[i].addEventListener('mouseover', () =>{
  listItems[i].style.textTransform = 'uppercase';
});

listItems[i].addEventListener('mouseout', () =>{
  listItems[i].style.textTransform = 'lowercase ';
});
}

1 Answer

There shouldn't be any difference in those methods, I like your method actually, less repetition.

You can even use ES6 syntax to make the for loop even shorter, like this:

for (let item of listItems){
  item.addEventListener('mouseover', () =>{
    item.style.textTransform = 'uppercase';
  });

  item.addEventListener('mouseout', () =>{
    item.style.textTransform = 'lowercase ';
  });
}

also i'd suggest using indentation to make your code more readable. It is not necessary for the code to function, but it makes it so much easier to read.