1

I want to print the value of textbox when i click of button. But it throws nullpointerexception. I also need to keep some value in the textbox i dont understand why?. Whatever i type in textbox and when i click on buttom i need to print the value of textbox What is the issue?

Below is my code:

ts file

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-mypage',
  templateUrl: './mypage.component.html',
  styleUrls: ['./mypage.component.scss']
})
export class mypageComponent implements OnInit {

  constructor(private router: Router) {

  }

  ngOnInit() {
  }



  myFunc() {

      var num1 = ((document.getElementById("exchageRateDate") as HTMLInputElement).value);
      console.log(num1);
  }

}

HTML File

<br>Welcome.<br>
Place - <input type="text" value="Sydney" ng-model="placeId" />
<button (click)="myFunc(placeId)" formtarget="_blank">Test</button>

Error:

ERROR TypeError: Cannot read property 'value' of null
sTg
  • 4,313
  • 16
  • 68
  • 115
  • Usually manipulating the dom directly in Angular application is discouraged. I would use a template variable to reference the input and pass the value directly to myFunc. – Adrian Fâciu Jul 13 '18 at 09:58

3 Answers3

11

Seems like you forgot to add id in input field

<input id="exchageRateDate" type="text" value="Sydney" ng-model="placeId" />

Edit: Angular way to do it

As you are using Angular so I will suggest you a better way to do this using NgModel

Try this

<br>Welcome.<br>
Place - <input type="text" value="Sydney" [(ngModel)]="placeId" />
<button (click)="myFunc(placeId)" formtarget="_blank">Test</button>

In component:

myFunc(num1) {
  console.log(num1);//here you will get input value through ng-model
}
Saurabh Agrawal
  • 7,581
  • 2
  • 27
  • 51
3

You need to set id of input tag remove ng-model because it's a angularjs(1.x.x) not angular(2/4/5/6/7/8)

In html

<br>Welcome.<br>
 Place - <input id="exchageRateDate" type="text" value="Sydney"  />
 <button (click)="myFunc()" formtarget="_blank">Test</button>

In typescript:

myFunc() {
    var num1 = ((document.getElementById("exchageRateDate") as HTMLInputElement).value);
    console.log(num1);
}

Here is working example: Get value of input tag using HTMLInputElement

Saurabh Agrawal
  • 7,581
  • 2
  • 27
  • 51
Sanoj_V
  • 2,936
  • 1
  • 12
  • 28
  • Why do you want to use "document.getElementById()" here when we can use a binding that would keep things sane. – Xonshiz Dec 26 '20 at 05:30
  • It would be better to use a "ViewChild" instead. It's much more closed to "Angular" approach. Check the answer [HERE](https://stackoverflow.com/a/48226955/2408212). – Xonshiz Dec 26 '20 at 05:40
0
<input type="text" class="textbox" id="Summary" name="Summary"placeholder="Summary" value="{{Item.summary}}">
(document.getElementById("Summary") as HTMLInputElement).value;
Rajat Jain
  • 134
  • 1
  • 6
  • 3
    Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written – WhatsThePoint Sep 02 '19 at 10:44