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 Functions Create Reusable Code with Functions Using Multiple return Statements

Andrew Terrett
Andrew Terrett
1,947 Points

Alert box is not popping up for me can someone check my code?

When I try and run the code the alert box is not popping up in the browser when I check the console it says uncaught typeerror cannot read properties of null.

can someone let me know where I'm going wrong here?.

function isFieldEmpty() {
    const field = document.querySelector('#info');
    if (field.value === ''){
        return true;
    } else {
        return false;
    }
}

const fieldTest = isFieldEmpty();

if (fieldTest === true) {
    alert("Please provide your information.");
}

2 Answers

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Rohald van Merode
Treehouse Staff

Hi Andrew Terrett 👋

This snippet of code looks great to me and should be working as expected 😃 This error usually means that you're trying to target an element that does not exist. Could you have a look at the index.html file and make sure that you've uncommented the input element on line 10 as Guil shows at 1:16?

Hope this helps! 🙂

Andrew Terrett
Andrew Terrett
1,947 Points

Hey!

Thanks for getting back to me! I have checked the input element and it is uncommented I know for sure its something I have done wrong I just really cant figure out what it is lol.

Rohald van Merode
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rohald van Merode
Treehouse Staff

Hm... I'm not entirely sure what could be the issue then, I've copy/pasted your code into a workspace on my end, uncommented the input field and adjusted the script path to link the correct .js file and everything was working for me 🤔

If you're still having issues with this feel free to create a snapshot of your workspace. By doing this it'll generate a link you can share for others to see your exact code. This makes it easier for us to see what might be causing the issue 🙂