0

super beginner here:

I having trouble figuring out how to correctly use this conditional statement for a placeholder. For some reason, it only executes one condition after clicking on submit (in this case, always true) whether or not there's a string typed into the placeholder.

This is the code I have so far:

function handleObj(task){
    p.textContent = 'My goal is to ' + task.toLowerCase()
    if (typeof p.textContent !== 'string'){
        h.textContent = 'Please type a goal'
        p.textContent = ' '
        submit.disabled = true;
    }
    };

TIA

taram414
  • 35
  • 7
  • If you assign a string to the `.textContent` and then check to see if it's a string... yes, it'll always give you a string. Check the `task` instead – CertainPerformance Jul 20 '22 at 01:18
  • Unfortunately, replacing it with task made no changes. I also tried removing .textContent on p and it only returned the !== regardless. – taram414 Jul 20 '22 at 01:36
  • Where do you call `handleObj`? – CertainPerformance Jul 20 '22 at 01:37
  • I put right here: .document.addEventListener("DOMContentLoaded", () => { addEventListener('submit', (e) => { e.preventDefault(); handleObj(e.target.goal.value) }) }); – taram414 Jul 20 '22 at 01:46
  • If the function runs and is targeting the proper element, checking whether `task` is the empty string does sound like it'd be your solution – CertainPerformance Jul 20 '22 at 01:50
  • Unfortunately, it always return true even when there’s nothing typed into the placeholder when replacing p.textContent with task – taram414 Jul 20 '22 at 01:54
  • Please edit a MCVE into your question with a live Stack Snippet so others can reproduce the problem too - that way it can be debugged effectively – CertainPerformance Jul 20 '22 at 01:54
  • Thanks for the help. The task advice was part of the solution. I just replace the boolean and just used ‘’ instead. – taram414 Jul 20 '22 at 02:27

0 Answers0