How can you verify the inputted age and execute the correct conditional statement?

Currently, I am going back to basics in JavaScript and realizing that my foundation is not as strong as I had assumed. It seems like I need a fresh perspective because I can't seem to pinpoint where the code error lies.

(A lot of the resources I found online mentioned using a fixed value for conditionals, such as 'var age = #', but did not address scenarios where it varied based on user input.)

Your assistance would be greatly appreciated! Thank you in advance!

const ageInput = document.querySelector("#ageInput");
const paraContent3 = document.querySelector(".paractice3-p");
const enter = document.querySelector(".enter");

function checkAge() {
  const age = ageInput.innerHTML;
  if (age < 10) {
    paraContent3.innerHTML = "You would roughly be in elementary";
  } else if (age < 14) {
    paraContent3.innerHTML = "You would roughly be in middle school";
  } else if (age < 18) {
    paraContent3.innerHTML = "You would roughly be in high school";
  } else {
    paraContent3.innerHTML = "You're out of school!";
  }
}

enter.addEventListener("keydown", function (e) {
  if (e.target == 13) {
    checkAge();
  }
});
#practice3 {
  display: flex;
  flex-direction: column;
  text-align: center;
}
#practice3 h1 {
  text-transform: uppercase;
}
#practice3 input {
  text-align: center;
  width: 30%;
  position: relative;
  left: 50%;
  transform: translate(-50%);
  padding: 10px;
  text-transform: capitalize;
}
#practice3 .enter {
  text-transform: uppercase;
  width: 20%;
  padding: 10px;
  margin-top: 10px;
  position: relative;
  left: 50%;
  transform: translate(-50%);
}
<section id="practice3">
  <h1>what is your age?</h1>
  <input id="ageInput" type="number" min="1" placeholder="enter your age">
  <p class= "practice3-p"></p>
  <button class="enter">enter</button>
</section>

Answer №1

It seems like there might be a mix of different elements in your code, possibly related to jQuery. I've made a few adjustments for you, but feel free to customize it further based on your needs. Let me know if you encounter any more issues.

<section id="practice3">
  <h1>What is your age?</h1>
  <input id="ageInput" type="number" min="1" placeholder="Enter your age">
  <p class="practice3-p" id="showMessage"></p>
  <button class="enter" onClick="checkAge()">Enter</button>
</section>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
   
<script>
function checkAge() {
    const ageInput = $("#ageInput").val();
    const paraContent3 = $("#showMessage");
    const enter = $(".enter");
    const age = parseInt(ageInput);
  if (age < 10) {
    paraContent3.text("You would roughly be in elementary");
  } else if (age < 14) {
    paraContent3.text("You would roughly be in middle school");
  } else if (age < 18) {
    paraContent3.text("You would roughly be in high school");
  } else {
    paraContent3.text("You're out of school!");
  }
}
/*
enter.addEventListener("keydown", function (e) {
  if (e.target == 13) {
    checkAge();
  }
});*/
  </script>

Answer №2

let userAge = ageInput.textContent;

updated to

let userAge = parseInt(ageInput.innerHTML);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for choosing input content when there is a single error class

I'm trying to determine if there is exactly one div with an error class. If so, I want to use the .select() method to select the content of the corresponding input (within that input's parent div). Any ideas on how to achieve this? This is my a ...

Stealthy access using the Facebook API log-in

I'm currently developing an app that features Facebook login functionality. I'm wondering if there's a way to keep a device authorized so that users don't have to go through the process of logging in with Facebook each time they use the ...

The element 'Component' cannot be utilized as a JSX component since its type 'ReactElement<any, any> | Component<{}, any, any>' is not a valid JSX element

I'm currently facing an issue while running "yarn build" and/or "npm run build" on a repository. The error persists for both commands. My goal is to build and deploy this code to an AWS environment, but I am stuck at resolving the errors that indicate ...

Bootstrap popover fails to function without any visible errors

Currently in the process of developing a website with the latest version of Bootstrap, I'm looking to implement a feature where a popover appears upon clicking a specific button. After including the necessary jQuery and popper.js files in my project, ...

Troubleshooting: JQuery dropdown selection not updating image display

I am trying to create a select menu that changes the car image when a user selects a different model, but for some reason it is not working. Here is what I have tried: <h2 class="model">A6 <img src="images/3.jpg" id="image" width="544" height="2 ...

PHP function - retrieving the beginning element in the array that is returned for every row

My current database setup involves a MySQL DB with approximately 100 rows. Within each row, there is a list of items stored in a column within the table. The challenge I am facing is extracting the first item from this list in each row. For example, if the ...

Refreshing page with ReactJS internationalization (i18n) capabilities

Currently, I am utilizing react-i18next within my Reactjs application. An issue that I am encountering is that whenever I switch the language, the app reloads and always reverts back to the main route. Is there a method to redirect to the same page or ch ...

Utilize Ionic and Angular to display the local storage value within a text box

I am in the process of developing an application using the Ionic framework. I am able to upload files to the server and receive a response, which I then save the file name in localstorage. However, I am facing an issue where I need to display this value in ...

$scope.apply is triggering both "catch" and "then" handlers

I am trying to ensure that the content of a page in Angular 1.6.2 and UI router is only displayed once it has been confirmed on the server that the user has the appropriate role/permissions. It seems like without using $scope.apply(), the catch() function ...

Issues with jQuery requests not working properly on the mobile application

Creating a mobile application for Android devices using Intel XDK has been a rewarding experience so far. I have been testing my PHP code on an emulator and local development server (127.0.0.1) through AJAX methods such as $.ajax(), $.post(), and $.get(). ...

Creating Conditional Vue Components

I am looking to dynamically create a list of actions, each of which is a component, based on a condition where the object variable store.plan is not empty. I attempted using v-if, which worked well for rendering but not for creating the component. However ...

The curtains fail to load when transitioning to a new page

Currently, I am using curtainjs on my website. The issue I am facing is that when I navigate from my landing page to the about page, curtainjs doesn't work initially. However, upon refreshing the page, everything starts working as expected. Below is ...

The controls for the HTML audio component are missing the download option on Safari

In my React application, I've declared an audio component with controls like the following: <audio controls> <source src={url} /> </audio> While this setup works perfectly in most browsers, it seems to have a bug when used in Safa ...

Issue with Angular2 Router not recognizing route for homepage

I recently incorporated the Angular2 Webpack Starter into my Angular2 app, which has been working great. However, after adding a fallback route to my app.routes.ts file and including its module (NotFoundModule) in app.module.ts, I encountered an issue wher ...

JavaScript: Only a single function call is successful

Recently, I encountered an issue with my registration form. Everything was working smoothly until I attempted to add a new onblur() event to a different text field within the same form. Surprisingly, this caused the original ajax call to stop functioning, ...

Having trouble getting two components to return in React

After successfully implementing the Hello World example by returning "Hello" and "world" from two different components, I encountered a problem with my code. In this specific code snippet, I am unable to return the Menubar component, although the Map compo ...

What is the process for refreshing information in VueJS?

<script> export default { data() { return { data: {}, dataTemp: {} } }, methods: { updateData() { let queries = { ...this.$route.query } this.data = { ...this.data, pID: queries.pid, s ...

Reset the form upon submission in AngularJS

Hey, I'm looking to reset the form values after a successful submission. How can I achieve this? <div ng-controller="employeelistController as listControl"> <div class="container form-group" ng-controller="addEmployee as addemp"> ...

Utilize Z-index to hide elements from view

Encountering an issue with hiding other div elements when one is visible. In the code snippet below, I aim to make Pacific Rim and World War Z disappear using z-index when Star Trek is visible. Here's my HTML code: <!doctype html> <html ...

Altering the color of a Fabulous Icon in real-time

I'm having trouble changing the color of an Awesome Icon with this code I created. Instead of getting the desired color, I am getting 'undefined' as a result. <script type="text/javascript"> function changeAIColor(idName) { alert ...