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
1
  • 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. Commented Jul 13, 2018 at 9:58

3 Answers 3

12

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
}
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Why do you want to use "document.getElementById()" here when we can use a binding that would keep things sane.
It would be better to use a "ViewChild" instead. It's much more closed to "Angular" approach. Check the answer HERE.
0
<input type="text" class="textbox" id="Summary" name="Summary"placeholder="Summary" value="{{Item.summary}}">
(document.getElementById("Summary") as HTMLInputElement).value;

1 Comment

Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written

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.