0

I have a textarea and one dropdown. I want whatever string i enters in textarea, that string will appear in the drop-down as an option. Suppose if I enter in textarea as 'zcxvbnuyt', then after pressing 'enter', this string would make the drop-down option.

And the choices I have to send on server must have a string with \n as separator between multiple choices.

Here is my HTML file:

app.component.html

<div class="col-sm-6">
     <textarea rows="4" 
               cols="50"
               class="form-control input-sm" 
               id="Choices" 
               [(ngModel)]="model.Choices">
     </textarea>
 </div>


<div class="col-sm-6">
     <select class="form-control input-sm" 
          [(ngModel)]="model.DefaultValue" 
          [ngModelOptions]="{standalone: true}"
          required>
          <option *ngFor="let c of defaultOptions" [ngValue]="c.value">{{c.value}}</option>
     </select>
</div>

app.component.ts

 export class model{
            Choices: string;
            DefaultValue:string;
    }

 defaultOptions= [
                { value: 'dummy1' },
                { value: 'dummy2' }
        ];
2
  • 1
    Can you add your typescript code as well..! Commented Sep 7, 2017 at 11:31
  • @DheerajKumar, i have updated my typescript file. Commented Sep 7, 2017 at 11:44

1 Answer 1

1

HTML

<div class="col-sm-6">
<textarea rows="4"
    cols="50"
    class="form-control input-sm"
    id="Choices"
    (keypress)="checkForNewLine($event.keyCode)" [(ngModel)]="model.Choices">
</textarea>
</div>


<div class="col-sm-6">
<select class="form-control input-sm"
[(ngModel)]="model.SelectedValue"
[ngModelOptions]="{standalone: true}"
required>
    <option *ngFor="let choice of defaultOptions" [ngValue]="choice.value">{{choice.value}}</option>
</select>

typescript

checkForNewLine(keycode){
if (keycode === 13 || keycode == 8 || keycode == 46){
    this.defaultOptions = [];
    let tmp = model.Choices.split('\n');
    tmp.forEach((data, index) => {
            this.defaultOptions.push({value: data});
        });
}
Sign up to request clarification or add additional context in comments.

2 Comments

It's working fine while adding. But also what I want is, while removing the string from the textbox(by pressing backspace/delete), then from the dropdown, that string should also be removed.
thanks for updating my code. made the changes. It should work with backspace and delete key now.

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.