0

Have to show one textbox only at a time, if click on toggle need to hide first textbox and need to show second textbox. If I click again need to show first textbox and hide second textbox. Tried below, but unable to display one textbox onload itself...

HTML:

 <div><input *ngIf="text1"
  id="test1"
  type="text"
  placeholder="Textbox1"
/>
</div>
<div>
<input *ngIf="text2"
  id="test2"
  type="text"
  placeholder="Textbox2"
/>
</div>

<button (click)="toggle()">Toggle</button>

.ts

 toggle() {
    this.text1 = !this.text1;
    if (this.text1) {
      this.text2 = false;
      this.text1 = true;
    } else {
      this.text2 = true;
      this.text1 = false;
    }
  }

Demo

2 Answers 2

2

You can use 1booleanItem

toggle() {
    this.text1 = !this.text1;
}
<div><input *ngIf="text1"
  id="test1"
  type="text"
  placeholder="Textbox1"
/>
</div>
<div>
  <input *ngIf="!text1"
  id="test2"
  type="text"
  placeholder="Textbox2"
/>
</div>

<button (click)="toggle()">Toggle</button>

Demo

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

Comments

1

Tried below, but unable to display one textbox onload itself

you can set the boolean of one of the text boxes to true:

public text1: boolean = true;

Using a single variable to track the visibility:

export class NgbdTypeaheadTemplate {
  public showText1: boolean = true;

  toggle() {
    this.showText1 = !this.showText1;
  }
}

and

<div><input *ngIf="showText1"
  id="test1"
  type="text"
  placeholder="Textbox1"
/>
</div>
<div>
  <input *ngIf="!showText1"
  id="test2"
  type="text"
  placeholder="Textbox2"
/>
</div>

2 Comments

yeah...I just realized it, thanks a lot :)
@Rajasekhar You can also refactor, see the updated answer.

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.