5

I wish to show the input field when "Others" option is selected on dropdown, but can't figure out how to-

Here is my code

   <div class="form-group">
      <label for="college">College:</label>
      <select class="form-control" id="college" ngModel name="college_name" required>
        <option>Ajay Kumar Garg Engineering College</option>
        <option>Others- Please Specify Below</option>
      </select>
    </div>
    <div class="form-group" *ngIf="showfield">
      <label for="name">Enter College Name:</label>
      <input type="text" class="form-control" id="name" ngModel name="other_college_name" required>
    </div>

showfield = false; is set in .ts file

1
  • @D.Simon You got me wrong, My purpose is to set that automatically based on option selection Commented Sep 6, 2017 at 11:35

3 Answers 3

12

In your component take a variable,

selectedValue: string = '';

Just assign selectedvalue to ngModel and use that value to display text field.

Also, options needs value attribute and it needs value to store in ngModel

<div class="form-group">
      <label for="college">College:</label>
      <select class="form-control" id="college" [(ngModel)]="selectedValue" name="college_name" required>
        <option value="college">Ajay Kumar Garg Engineering College</option>
        <option value="others">Others- Please Specify Below</option>
      </select>
 </div>
    <div class="form-group" *ngIf="selectedValue == 'others'">
      <label for="name">Enter College Name:</label>
      <input type="text" class="form-control" id="name" ngModel name="other_college_name" required>
    </div>
Sign up to request clarification or add additional context in comments.

8 Comments

just a note, you will need to create the variable selectedValue inside the *.component.ts file.
yes thats obviously needed, let me add that to be more precise.
Yup, just mentioning it because the OP didn't know about [(ngModel)], just covering all the bases :)
Actually you don't need to. You can bind a variable to ngModel and use it only in the HTML ! I often do that with checkboxes.
@Sravan never said otherwise. I'm just stating the fact that you don't need to do so.
|
4

You can do like so

   <div class="form-group">
      <label for="college">College:</label>
      <select class="form-control" id="college" [(ngModel)]="collegeName" name="college_name" required>
        <option value="AKGEC">Ajay Kumar Garg Engineering College</option>
        <option value="other">Others- Please Specify Below</option>
      </select>
    </div>
    <div class="form-group" *ngIf="collegeName === 'other'">
      <label for="name">Enter College Name:</label>
      <input type="text" class="form-control" id="name" ngModel name="other_college_name" required>
    </div>

2 Comments

Thanks! Worked like a charm
No problem, mark at least one of the answers as resolved !
0

Your options must have a value associated with them, the selected options value will get set to the model associated with your dropdown select. You can check that for show/hide as follows:

<div class="form-group">
          <label for="college">College:</label>
          <select class="form-control" id="college" [(ngModel)]="selectedVal" name="college_name" required>
            <option value="ajay">Ajay Kumar Garg Engineering College</option>
            <option value="others">Others- Please Specify Below</option>
          </select>
        </div>
        <div class="form-group" *ngIf="selectedVal==='others'">
          <label for="name">Enter College Name:</label>
          <input type="text" class="form-control" id="name" ngModel name="other_college_name" required>
</div>

Comments

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.