Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular and vanila JS - set input value

I would like to set input value (in Vanilla JS) in website created in Agnular.

when I try to do:

document.querySelector('my_input').value = 'value';

the value shows in input but searching doesn't see this value (form is submitted but it looks like my input is empty).

I see that there is event ngModelChange.

Is there any option to do something like that:

document.querySelector('my_input').dispatchEvent(new Event('ngModelChange', {value: 'value'}));

If I do that i get in input [object Event] and the form searching get this value.

Any ideas?

like image 874
Stepel Avatar asked May 19 '26 03:05

Stepel


1 Answers

I solved my problem. This code works for me:

document.querySelector('my_input').value = 'my_value';
document.querySelector('my_input').dispatchEvent(new Event('input'));
document.querySelector('my_input').dispatchEvent(new Event('blur'));

Thanks for you answers.

like image 158
Stepel Avatar answered May 21 '26 17:05

Stepel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!