0

I am trying to get the values of an input field and a dropdown in a function . But i get the error can not read the property of null.

languageSelected(name: ElementRef, languageSelect: ElementRef) {
    alert(languageSelect.nativeElement.value);
}

The view is below.

<div class="form-group row">
   <label class="col-md-3 form-control-label" for="name">{{'movieCategory.name'|translate}}</label>
   <div class="col-md-5">
      <input #categoryNameInput [disabled]="pageStatus==4" ngModel="{{movieCategory.name}}"  name="name" id="name" type="text" class="form-control" placeholder="{{'movieCategory.placeHolder.name'|translate}}">
   </div>
   <div class="col-md-3">
      <select #languageSelection class="form-control">
         <option value="tr">Türkçe</option>
         <option value="en">İngilizce</option>
         <option value="de">Almanca</option>
      </select>
   </div>
   <div class="col-md-1">
      <i (click)="languageSelected(categoryNameInput,languageSelection)" class="fa fa-plus fa-2x" style="margin-top:5px;margin-left:-15px;color:green"  aria-hidden="true"></i>
   </div>
</div>

Btw when i try alert(languageSelect) instead , it alerts [object HTMLSelectElement] which is okay i think.

n00dl3
  • 21,213
  • 7
  • 66
  • 76
rematnarab
  • 1,277
  • 4
  • 22
  • 42

1 Answers1

1

You need to add .value to the console.log value as by deafult it passes in the whole html markup where as you need the value.

languageSelected(categoryNameInput,languageSelection){
    console.log(categoryNameInput.value);
    console.log(languageSelection.value);
}
Rahul Singh
  • 19,030
  • 11
  • 64
  • 86
  • thanks for the quick answer. What if i also want to access to name of the selection. Lets say "İngilizce" when "en" is selected ? – rematnarab Sep 18 '17 at 09:52
  • @rematnarab its better you use angular forms for the same [Reactive forms](https://rahulrsingh09.github.io/AngularConcepts/reactive) and [template driven](https://rahulrsingh09.github.io/AngularConcepts/template) . Also you can check this [ans](https://stackoverflow.com/a/39409050/2708210) – Rahul Singh Sep 18 '17 at 09:56