0

How do I select the textfield element "member-name"?

function revealPaywall (){
  const paywall = document.getElementById('paywall')
  const name = localStorage.getItem("value");
//TO WRITE MULTILINE HTML IN JS, USE BACKTICKS `` TO CREATE A TEMPLATE LITERAL
  if (localStorage.getItem("value") === null){
    paywall.innerHTML =
      `
      <h1>This Is An Example Of A Paywall<h1>
      <p>If you enter a name, it will be saved to the localStorage. To bypass just enter your name and click the "submit" button<p>
        <label for="name">Member Name:</label>
        <input type="text" id="member-name">
        <br><br><br>
        <button onclick="paywallComplete()">SUBMIT</button>
      `
  }
}
function paywallComplete (){
  const paywallName = document.querySelector("#member-name");
  if (paywallName.value === null){
    window.alert('hi');
  }

Am I selecting it correctly? if so why am I not getting a 'hi' alert when clicking on the submit button. I press it with the textfield empty so I'm assuming the paywallName.value === null should be true. I tried querySelector(#member-name) & querySelector(member-name) to no avail.

2
  • paywallName.value === null should be true No. Empty string is not exactly equal to null. Commented Jan 15, 2024 at 18:22
  • Try printing paywallName.value to see what it actually is to debug your if statement: console.log(typeof paywallName.value, paywallName.value). Commented Jan 15, 2024 at 23:38

1 Answer 1

-2

Why don't you add elements with DOM object? For example, in order to add the input element, add this code:

var input = document.createElement('input');
input.type = "text";
input.id = "member-name";
paywall.appendChild(input);

Also you can summarize these codes by jquery.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.