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 (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,209 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.