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

Conditional Statement wont run !

I created an input section where the username is entered and the conditional statement checks if user name === 'USERNAME2000' then it appends some text to the DOM. But the console keeps telling me that i cant add .toUpperCase() to my condition? i dont know why This is my code

// DOM ELEMENTS let container = document.querySelector('.container'); let container2 = document.querySelector('.container-fluid'); container2.style.display = 'none'; let button = document.querySelector('.btn'); //BUTTON EVENT LISTENERS button.addEventListener('click', () => { let userName = document.getElementById('userName').value;

if (userName.toUpperCase() === 'USERNAME2000') {
    let h2 = document.createElement('h2');
    h2.innerHTML = 'another one';
    container.append(h2);
} else {
    alert('Thats not the username');
}

})

Cameron Childres
Cameron Childres
11,818 Points

Curious -- I just copied your code for a quick test and it's working fine for me. Could you post the HTML? Is this the full script you're working with?

Tip: use the markdown cheatsheet linked below the comment box to format your code so we can read it easier, like this:

```javascript

code goes here

```

1 Answer

Robert Manolis
STAFF
Robert Manolis
Treehouse Guest Teacher

Hey Obe, it's hard to say with an absolute certainty, but generally, when you get a message saying that that you can't use a method like toUpperCase(), it's usually because there's an issue with that ever element you're trying to call that method on.

So just before your conditional, try logging out userName to the console. Hopefully that will provide some insight into the problem. :thumbsup: