Home Angular 10 Angular 9/10 select2 get value onchange event

Angular 9/10 select2 get value onchange event

by therichpost
10 comments
angular 10 select2 get value

Hello to all, welcome to therichpost.com. In this post, I will tell you, Angular 9/10 select2 get value onchange event.

Angular 10 came. if you are new in angular then please check below two important links:

Angular for Beginners

Angular 10 basics tutorials

Post Working:

In this post, I am showing select2 into my Angular 10 and get value onchange event..
angular 10 select2 get value
Angular 9/10 select2 get value onchange event

Here is code snippet and please use carefully:

1. Very first, you need to below command into your Angular 10 application:

npm install select2
npm install jquery --save

2. Now you need to add below code into your angular.json file:

"styles": [
       "node_modules/select2/dist/css/select2.min.css",
              ...
          ],
"scripts": [
       "node_modules/jquery/dist/jquery.min.js", 
       "node_modules/select2/dist/js/select2.min.js"
              ...
      ]

 

3. Now you need to add below code into your app.component.ts file:

...
declare let $: any;
...


export class AppComponent {
...
   
   ngOnInit() {

    
    $('.js-example-basic-single').select2();
    $('.js-example-basic-single').on("change", function(){
	   alert($(this).val());
    });

  }

...


}

 

4. Now you need to add below code into app.component.html file:

<select class="js-example-basic-single" name="state" style="width: 150px;">
  <option value="AL">Alabama</option>
  <option value="WY">Wyoming</option>
</select>

 

 

This is it and if you have any kind of query then please do comment below.

Jassa

Thank you.

You may also like

10 comments

Waqas July 8, 2020 - 8:32 pm

Thank you dost it’s working but I need to call with this value when Alert generate another ts function which not work please help

Reply
Waqas July 8, 2020 - 9:02 pm

Thanks dost I did it thank you so much but one question is that I want to bind default value to select 2 when update the record

Reply
Waqas July 8, 2020 - 10:19 pm

Thanks dost I did it as well thank you so much for great help please if u don’t mind share a whts up number

Reply
Ajay Malhotra July 9, 2020 - 8:29 am

Yes sure: +919814419350

Reply
Ucup December 16, 2020 - 10:49 am

Doesn’t work on my project. Console log showing me “ERROR TypeError: $(…).select2 is not a function”

Reply
Ajay Malhotra December 16, 2020 - 10:53 am

I think you missed jQuery to install!!

Reply
Alejandro December 3, 2021 - 2:25 pm

Yes, i’ve installed jQuery and it doesnn’t work!

Reply
Ajay Malhotra December 6, 2021 - 4:03 am

Please see its latest post with angular 12.

Reply
Helper function January 16, 2021 - 2:13 pm

On HTML:

On TS:
onChangeItem(value:any) {
console.log(‘value’, value); //You will the the changed value here
}

Reply
Ajay Malhotra January 16, 2021 - 2:16 pm

Great.

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.