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

Jiten Mehta
Jiten Mehta
Front End Web Development Techdegree Student 7,943 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

Zimri Leijen
Zimri Leijen
6,955 Points

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.