Display information on a web page based on user input using HTML

I've hidden other p tags and divs using display:none,

How can I make them visible after inputting my name?

I'd like to type in my name and reveal all the content within the previously hidden div

<form method="post">
  <p>
    <input type="text" name="myname" />
  </p><label>
    <input type="submit" name="submit" value="Enter your names" />
    </label>
</form>
<div style="display:none;">
  <p>Enter your age to access this site</p>
  <p>Mr. MyName</p>

  <div>
    Waiting for your age to enter the site </div>
  <input type="text" name="age" />
</div>

Answer №1

To make your work easier, consider utilizing JQuery for DOM manipulations and listening to events like the blur event on input fields to display content dynamically.
For example, type your name in an input field and then click outside the input area to see the content appear.

$('input[name="myname"]').blur(function() {
  $('div').css('display', 'block');
});
<!DOCTYPE html>
<html lang="en">

<head>
  <title>title</title>
  <meta charset="utf-8">

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
  <form method="post">
    <p>
      <input type="text" name="myname" />
    </p>
    <label>
        <input type="submit" name="submit" value="Enter your names"/>
    </label>
  </form>
  <div style="display:none;">
    <p>enter your age to enter this site</p>
    <p>mr.myname</p>

    <div>
      waiting for your age to enter site
    </div>
    <input type="text" name="age" />
  </div>
</body>

</html>

Answer №2

// 1. Make sure jQuery is included in your code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

// 2. Modify the following line 
<input type="text" name="age" />
To
<input type="text" name="age" id="age" />

// 3. Insert this code where you want to display the age
<span id="printAge" style="color:green"></span>

// 4. Place this code before closing <body> tag
<script>
 $(document).ready(function(){

    $('#age').keyup(function () { 
         var ageText = $(this).val();
         $('#printAge').html(ageText);
    });
 });
</script>

If you encounter any further issues, feel free to reach out @Chris G

Answer №3

If you're looking to accomplish this task, consider the following method:

$('form').submit(function(e){
  $('#txtname').text($('[name=myname]').val());
  $('#container').show();
  e.preventDefault();// This is solely for demonstration purposes
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="post">
<p>
  <input type="text" name="myname" />
</p
><label>
<input type="submit" name="submit" value="Enter your names" />
</label>
</form>
<div id="container" style="display:none;">
<p>enter your age to enter this site</p>
<p id="txtname"></p>

<div>
   waiting for your age to enter site  </div>
  <input type="text" name="age" />
</div>

Answer №4

Are you attempting to reveal the hidden div once the user clicks the submit button? If I'm mistaken, please feel free to correct me.

Start by assigning a class of "hidden-div" to the hidden div. Next, utilize jQuery to detect when the submit button is clicked with the following code:

// locate the element with the class hidden-div
var hiddenDiv = $('.hidden-div');
// set up an event listener on the submit button
$('button[type=submit]').click(function(e) {
   // prevent the default action which submits the form
    e.preventDefault();

    // display the hidden div
    hiddenDiv.show();
});

To include jQuery, you must import it; this can be accomplished by adding a script tag just before the closing </body> tag

<script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>

I trust that this meets your requirements.

Answer №5

Do you think something along these lines would work?

$("[name=myname]").on("input",function() { 
  $("#otherstuff").toggle(this.value=="Fred Brown"); 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="post">
  <p>
    <input type="text" name="myname" />
  </p><label>
    <input type="submit" name="submit" value="Enter your names" />
    </label>
</form>
<div id="otherstuff" style="display:none;">
  <p>enter your age to enter this site</p>
  <p>mr.myname</p>

  <div>
    waiting for your age to enter site </div>
  <input type="text" name="age" />
</div>

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

Ensure that only the dropdown menu that is clicked within a Vue loop opens

Hey, I'm having an issue with my dynamically created drop-down menus. I can display them correctly using v-Show, but the problem is that when I click on one element, they all open below my code. <div :class="{inizio : utenteAttivo.nome === con ...

Is there a way to get rid of the "bouncing effect" on my button while using an inline floating-label text input?

When an input with a floating label (Bootstrap 5) is inline with other elements, the elements may appear to jump up and down depending on the position of the floating label. https://i.sstatic.net/kDxBo.gif <link href="https://cdn.jsdelivr.net/npm/bo ...

In my attempt to assess the correlation between value 1 and a value in the preceding object, I am utilizing the *ngFor directive

Attempting to compare 2 entries in an *ngFor loop. The code should compare the value at the current object to a value at the previous object. <ng-container *ngFor="let item of s_1.comments[0]; index as b"> <article class="message i ...

Can you explain the process of gathering texts based on CSS selector?

I wanted to place texts next to the div-class as shown in the following code snippets. <div class="review-contents__text">소재가 좀 저렴해 보이지만 그래도 입으면 휠씬 나아보여요</div> Initially, I wrote a code ...

Updating Github pages requires clearing the cache first

As I work on my first website using GitHub pages, I've noticed that it can be frustrating to constantly clear the cache or open an incognito window whenever I add something new. I'm thinking about incorporating Jekyll into my workflow so I can t ...

What is the best method for applying classes to a collection of DOM elements with jQuery?

I attempted two different methods of iterating through the arrays, but encountered the same error: addClass is not function First attempt using a for loop: function game(){ var cards = $('.card'); function initialize(){ for ...

Implementing user authentication in node.js with passport

Having trouble with a basic login system using passport. I keep getting an error when logging in with the correct credentials: Error: Express 500 TypeError - Cannot read property 'passport' of undefined Oddly enough, everything works fine when ...

Prevent selection based on JSON information

I am utilizing the Jiren filter library to sort through JSON data. If a particular filter criteria does not match any of the results, I would like to disable that option in the select dropdown. For instance, if "Silversea Expedition" is not found in my re ...

Angular successfully compiled without any issues despite the explicit cast of a number into a string variable

As I delve into the initial concepts of Angular, I have come across a puzzling situation. Here is the code snippet: import { Component } from '@angular/core'; @Component({ selector: 'sandbox', template: ` <h1>Hello {{ nam ...

Some mobile web browsers are experiencing difficulties when trying to load a background video coded in html5

There is a full background HTML5 video set to autoplay on my website. However, there seems to be an issue with some iOS mobile devices using Safari as the video fails to load properly at times. The error message displayed is: https://i.stack.imgur.com/D4X ...

Creating sparse fieldset URL query parameters using JavaScript

Is there a way to send type-related parameters in a sparse fieldset format? I need help constructing the URL below: const page = { limit: 0, offset:10, type: { name: 's', age:'n' } } I attempted to convert the above ...

Error: The "res.json" method is not defined in CustomerComponent

FetchData(){ this.http.get("http://localhost:3000/Customers") .subscribe(data=>this.OnSuccess(data),data=>this.OnError(data)); } OnError(data:any){ console.debug(data.json()); } OnSuccess(data:any){ this.FetchData(); } SuccessGe ...

Is there a way to alphabetically and numerically organize a table in vue.js?

Currently, I am implementing sorting functionality for a table using vue.js. While I have successfully achieved ascending sorting for numbers, I am facing challenges with getting the descending and alphabetical sorting to function properly. Below is the H ...

Learn how to display each element in a list one by one with a three-second interval and animated transitions in React

Let's consider a scenario where we have a component called List: import React, { Component } from 'react'; class List extends Component { constructor() { super(); this.state = { list: [1, 2, 3, 5] } ...

Steps for opening a modal in a react native application when a button is clicked

Exploring the realm of react native, I face a challenge in triggering a modal on button click. I've attempted to implement the following code snippet to achieve this goal:- openHeaderModal = () => { <ModalDropdown options={["H1", "H ...

Explanation for the strange floating math in JavaScript - Understanding the IEEE 754 standard for laymen

When it comes to JavaScript and working with floating point numbers, I always feel a bit lost. Dealing with decimals makes me nervous because I'm never quite sure what's happening behind the scenes. If only I understood how the IEEE 754 standard ...

Error encountered while using VueJS: The Register component was not recognized as a

Encountering issues with the registration component in VueJS. I acquired the component from https://github.com/wanxe/vue-button-spinner via npm install. Subsequently, I integrated the code into my Laravel 5.5 app.js file. The contents of my app.js: requi ...

Module not found in Node.js environment (webpack)

I seem to be encountering an issue with loading any modules. Despite reinstalling my operating system, I continue to face the same error when attempting to use any module. I have tried reinstalling node, clearing the cache, and more. To view the code, pl ...

Create a log table specifically for tracking changes made to the drop-down menu

I need to create a Change log table that will track any changes made in the drop-down menu. For instance, I am working on a worksheet with a select menu called Results which includes options like Positive, Negative, Unknown. I want the system to log any ch ...

The jQuery selector is unable to detect the Bootstrap modal when it is hidden

I attempted to change the value of a jQuery selector within a bootstrap modal, but unfortunately, my efforts were unsuccessful. Even after utilizing the Chrome console, I was unable to modify the value as intended. Could someone offer guidance on how to re ...