Giter VIP home page Giter VIP logo

int-u4l1-23-24-student-exercises's Introduction

Lesson 4.1: Storing Input

Input Fields

Input fields are HTML elements that allow you to collect data from the user. They are typically found within forms on web pages.

Here's an example of an HTML input field for an email:

<input class="user-input" type="email" placeholder="Enter your email">

The .value Property

In JavaScript, the .value property is used to get the current value of an input field. This is essential for collecting user input.

Event Listeners

Event listeners are used to wait for events to occur, like a button click, and then perform actions in response.

Adding an Input Field and Button in HTML

Here's how you can add an input field and a button to your HTML:

<input class="user-input" placeholder="Email">
<button>Login</button>

Adding an Event Listener in JavaScript

To make your webpage interactive, you add an event listener to the button:

let button = document.querySelector("button");
button.addEventListener("click", function() {
  let userInput = document.querySelector(".user-input").value;
  console.log(userInput);
});

When the button is clicked, the value from the input field will be printed to the console.

Displaying User Input on the Webpage

You can use .innerHTML to display the user input within a different HTML element:

button.addEventListener("click", function() {
  let userInput = document.querySelector(".user-input").value;
  document.querySelector(".display").innerHTML = userInput;
});

Converting String Input to a Number

Since inputs are returned as strings, you might need to convert them to numbers for calculations:

let num = Number(document.querySelector(".user-input").value);

Complete Example

Here’s a complete example of HTML and JavaScript code that creates an input field, listens for a button click, and displays the input on the webpage:

<!-- HTML -->
<input class="user-input" placeholder="Type something...">
<button>Submit</button>
<div class="display"></div>
// JavaScript
let button = document.querySelector("button");

button.addEventListener("click", function() {
  let userInput = document.querySelector(".user-input").value;
  document.querySelector(".display").innerHTML = userInput;
});

int-u4l1-23-24-student-exercises's People

Contributors

cn-curriculum avatar cn-mika avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.