How can I update the content of a span in Angular 2 by clicking a div, and then change the text above it when the span is filled?

As a newcomer to this community with basic English proficiency, I hope to communicate effectively. My journey into learning Angular 2 has led me to a specific question:

I've set up a keyboard with four spans above it, each intended to hold a digit that corresponds to a key on the keyboard. Whenever one of these keys (each represented by a div) is pressed, I'd like the respective number to display in one of the spans. However, I'm unsure how to achieve this functionality. Additionally, after inputting four numbers and filling the spans, I aim to have the text above the spans change.

The HTML structure:

<div class="container-fluid fixed-heading">
   <div class="back-button">
     <img src="/assets/images/icon_arrow_left.png" (click)="back()" />
   </div>
   <app-main-app-header [heading]="'SETTINGS.SECURITY.PIN.HEADING'">{{"SETTINGS.SECURITY.PIN.HEADING"|translate}}</app-main-app-header>
</div>

<div class="container-fluid component">

  <div class="row pin text-center">

    <div>
      <span>Here I would like to have the text</span>
    </div>

    <div class="container-fluid">
      <div class="col-xs-3">
        <span></span>
        <div class="line"></div>
      </div>
      <div class="col-xs-3">
        <span></span>
        <div class="line"></div>
      </div>
      <div class="col-xs-3">
        <span></span>
        <div class="line"></div>
      </div>
      <div class="col-xs-3">
        <span></span>
        <div class="line"></div>
      </div>
    </div>

  </div>

  <div class="row text-center">
    <div class="col-xs-4 cursor-pointer">
      <span>1</span>
      <p>&nbsp;</p>
    </div>
    <div class="col-xs-4 cursor-pointer">
      <span>2</span>
      <p>ABC</p>
    </div>
    <div class="col-xs-4 no-border-right cursor-pointer">
      <span>3</span>
      <p>DEF</p>
    </div>
  </div>

  <div class="row text-center">
    <div class="col-xs-4 cursor-pointer">
      <span>4</span>
      <p>GHI</p>
    </div>
    <div class="col-xs-4 cursor-pointer">
      <span>5</span>
      <p>JKL</p>
    </div>
    <div class="col-xs-4 no-border-right cursor-pointer">
      <span>6</span>
      <p>MNO</p>
    </div>
  </div>

  <div class="row text-center">
    <div class="col-xs-4 cursor-pointer">
      <span>7</span>
      <p>PQRS</p>
    </div>
    <div class="col-xs-4 cursor-pointer">
      <span>8</span>
      <p>TUV</p>
    </div>
    <div class="col-xs-4 no-border-right cursor-pointer">
      <span>9</span>
      <p>WXYZ</p>
    </div>
  </div>

  <div class="row text-center" id="no-border">
    <div class="col-xs-4 bg-color cursor-pointer">
      <span>&emsp;&ensp;&ensp;.</span>
    </div>
    <div class="col-xs-4 cursor-pointer">
      <span>0</span>
    </div>
     <div class="col-xs-4 no-border-right bg-color cursor-pointer">
      <span class="glyphicon glyphicon-remove-sign"></span>
    </div>
  </div>

</div>

CSS styling:

.fixed-heading{
  left: 0;
  right: 0;
  margin: auto;
  max-width: 375px;
  position: fixed;
  width: 100%;
  z-index: 25;
  border-bottom: 1px inset seashell;
}

.back-button{
  cursor:pointer;
  position: absolute;
  top: 9px;
}

.pin{
  background-color: #f2f2f2;
  height: 300px;
}

.row{
  border-bottom: 1px inset;
  padding-bottom: -2px;
  font-size: xx-large;
}

.row.pin{
  border-bottom: none;
  border-right: none;
  font-size: small;
  padding: 80px;
}

p{
  font-size: small;
  margin-top: -8px;
}

.col-xs-4{
  border-right: 1px inset;
}

#no-border{
  border-bottom: none;
}

.no-border-right{
  border-right: none;
}

.bg-color{
  background-color: #f2f2f2;
}

.line{
  border: 2px solid grey;
  width: 25px;
}

.row.pin span{
  margin-left: 8px;
}

.glyphicon.glyphicon-remove-sign{
  margin-top: 5px;
}

My current setup utilizes Bootstrap 3.3.7 and Angular 4.2.2.

Answer №1

It seems like you are looking to develop a functionality similar to a PIN code input. One suggestion would be to utilize keyboard input and take advantage of angular's built-in features as outlined here.

To address your specific query, here is one possible approach:

// Within your component

public textThatChanges = 'previous text';
public input: string = '';

click(numberClicked: string) {
  if (this.input.length < 4) {
    this.input += numberClicked;
  }
  else if (this.input.length === 4) {
    this.textThatChanges = 'new text';
  }
}

// In the template
<!-- This span will update after 4 clicks -->
<span>{{ textThatChanges }}</span>

<!-- Show 4 separate elements, each displaying 1 digit -->
<span>{{ input.charAt(0) }}</span>
<span>{{ input.charAt(1) }}</span>
<span>{{ input.charAt(2) }}</span>
<span>{{ input.charAt(3) }}</span>

<!-- Repeat for each clickable div -->
<div (click)="click('1')"><span>1</span></div>
<div (click)="click('2')"><span>2</span></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

Developing an Addon for Firefox using XUL and JavaScript

I am interested in creating a Firefox Addon that dynamically generates a button in the webpage's DOM when a specific page like www.google.com is loaded. This button, when clicked, will redirect to another page such as www.stackoverflow.com. It is imp ...

Unraveling the Mystery of the Undefined Parameter in Angular Observables

I am facing an issue with the Observable parameter in my code. I have a consultService that contains the functions consult() and response as a parameter. The function sendConsultRequest() is called before using the response parameter. Although the sendCons ...

Pseudo-element fails to display in React when applied to a paragraph tag, even with display block property set

I am experiencing an issue where the pseudo element ::after is not appearing in my browser. I am currently working with React.js and Material UI's makeStyles. Here is the code snippet causing the problem: modalTitle: { borderBottom: '2px sol ...

Error message appears when trying to render a shallow mock of a React.Component that extends MyInterface with any type

Encountering an Issue with Component Mocking When attempting to mock a component, I am receiving the following error message: "Conversion of type '{ props: { index: number; AssignmentTitle: string; AssignmentDescription: string; AssignmentUtilizedHou ...

Form in HTML with Automatic Multiplication Functionality in pure JavaScript

I'm struggling with integrating a simplified HTML form with JavaScript to dynamically adjust and multiply the entered amount by 100 before sending it via the GET method to a specific endpoint. Below is the HTML form: <body> <form method= ...

Updating a text input's placeholder using Bootstrap4

Bootstrap 4 provides classes like text-left and text-right for aligning text inside an input field. Is there a Bootstrap-friendly method to only align the placeholder text in an input without affecting the alignment of the actual text? ...

The SharePoint 2010 standard model dialogs seamlessly blend transparent printing with the background content

When using Sharepoint 2010, each list item you edit will open in a new modal dialog box by default. This dialog box appears as a new div with the class of ms-dlgContent. It also creates a div with the class of ms-dlgOverlay which acts as a grey overlay di ...

Animate the background of a table cell (td) in React whenever a prop is updated

I have a dynamic table that receives data from props. I would like the background animation of each cell (td) to change every time it receives new props. I've tried creating an animation for the background to indicate when a cell is updated, but it on ...

What are some effective methods for organizing HTML in a clean and orderly manner?

I have limited experience with HTML, but I need to layout a form in a precise manner similar to WinForms. Here is my attempt using the little CSS and HTML knowledge I possess: The issue lies in everything inside the white area being ABSOLUTELY LAID OUT .. ...

Unable to locate a resolution for the error message "Warning: Task "uglify" not found"

Below is the content of my Gruntfile.js: module.exports = function(grunt) { require('load-grunt-tasks')(grunt); grunt.initConfig({ uglify: { start: { files: { 'js/script.min.js': ['js/script.js&a ...

Run a single feature file in Protractor by utilizing a specific tag

Is it possible to run just one feature file in Protractor? I am aware that I can specify the file in protractor.conf.js, but I have come across a different solution involving the use of a tag: To single out a specific feature file, you would include a tag ...

Utilize NestJS to retrieve information from an observable gRPC service

One of the challenges I am facing is using gRPC service to facilitate communication between my microservices. Specifically, when receiving a response from the Grpc service, I need to apply some modifications and additional functionality before returning a ...

Exploring the latest features in Bootstrap 4 Alpha and enhancing your

Rumors have it that Bootstrap 4 will make its debut during the year 2016. When duty calls for an upgrade, is a complete re-write truly the best solution? Tackling such a project from Boostrap 2 to 3 was no small feat, and I find myself hesitant to take on ...

Updating the class of a div based on a checkbox being checked or unchecked

I am attempting to apply the "isChecked" class to the parent div when a checkbox is checked, using the ngClass directive. Here is the HTML: <div ng-class="{isChecked: (isChecked_1 == 'true')}"> <input type="checkbox" id="check_ ...

An obstacle prevents the code injection process in the Chrome extension when a button is pressed

My basic chrome extension is not functioning correctly. More specifically, I am encountering an issue where the alert box does not appear when I click on the button in the popup. Here are the contents of my manifest.json file: {"manifest_version": 2, "n ...

Combining Three.js with iFrame for a mix of WebGL and CSS3D

After some experimentation, I successfully created a dynamic page that seamlessly integrates WebGL and CSS3D (with valuable guidance from this helpful SO post). To add an extra touch, I included an iframe: However, I noticed that the functionality to inte ...

Dynamically setting attributes with ngFor iteration

I am attempting to dynamically set the selected attribute based on matching index values. <select class="form-control" disabled> <option *ngFor="let agency of request.agencyList" [attr.selected]="request.agencyIndex == agency">{{agency}}{{re ...

Is it possible to extend a DIV to 100% without affecting the width of its parent element set to auto?

Imagine having a DIV with multiple inner-DIVs: <div id="parent"> <div id="a"></div> <div id="b"></div> </div> All the inner-DIVs need to have the same width, which should be equal to the widest DIV. The parent elemen ...

Enhance your viewing experience by magnifying a specific element, all while maintaining full control to navigate

Is it possible to create a zoom effect on a webpage that focuses on one specific element while still allowing for navigation and scrolling? I have searched online for plugins like Fancybox and Zoomooz, but none of them offer the functionality I need. I sp ...

Tips for creating a Calculator with AngularJS

I am encountering issues while trying to develop a calculator using AngularJS. Below is the HTML code I have written: <!doctype html> <html class="no-js" lang=""> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-com ...