Obtain the input value from a modal and receive an empty string if no value

Utilizing ng-multiselect-dropdown within my bootstrap modal allows users to choose multiple products. Each time an item is selected (onItemSelect), a new div is inserted using the jQuery method insertAfter(). This new div displays the quantity of the selected product by generating a unique id starting with 'p' combined with the product id (idProduto). This approach enables me to remove the div onItemDeselect and retrieve the inputted value using the val() method in jQuery.

The issue arises when attempting to fetch the field value, as it returns an empty string.

Snippet from the onItemSelect method:

onItemSelect(item: any){
    if (item){
      this.itemCompra = item;
      $('<div id="p'+this.itemCompra.idProduto+'" class="form-group"><label for="exampleFormControlInput1">Quantidade de '+this.itemCompra.nome+'</label><input id="p'+this.itemCompra.idProduto+'" class="form-control" formControlName="quantidade" type="number" min="1" [(ngModel)]="compra.quantidade'+this.itemCompra.idProduto+'" [ngClass]="{ '+!this.inv+' : submitted && f.quantidade.errors }"><div *ngIf="submitted && f.quantidade.errors" class="invalid-feedback"><div *ngIf="f.quantidade.errors.required">Escolha a quantidade</div></div></div>').insertAfter('#bodyCompra form>div:last');
      this.cont++;
    }
} 

I am attempting to fetch the value for use in another function. The snippet below shows how I am trying to retrieve the value within a loop:

while(i<this.compra.ps.length){
  console.log($('#p'+this.compra.ps[i]["idProduto"]).val());
  //this.compra.ps[i]["quantidade"]=$('#p'+this.compra.ps[i]["idProduto"]).val();
  i++;
}

this.compra.ps represents an array obtained from my template utilizing [(ngModel)].

Answer №1

Issue Resolved! The problem was caused by having two ID's with the same name, resulting in val() returning an empty string. By updating the second ID, everything is now functioning correctly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Obtain a Service instance using the console

Back in the days of Angular 1, accessing the injector through the console was a convenient way to retrieve any service: angular.element(document.querySelector('html')).injector().get('MyService') This feature proved to be very helpful ...

Alphabetically sorting objects in an array using Angular

If your TypeScript code looks something like this: items: { size: number, name: string }[] = []; ngOnInit(): void { this.items = [ { size: 3, name: 'Richard' }, { size: 17, name: 'Alex' }, ...

Having trouble sending HTTP requests in Angular 6

I am currently facing an issue in my Angular application while trying to send an HTTP POST request to a Spring RESTful API. Despite my attempts, I have not been able to succeed and I do not see any error response in the browser console. Below is a snippet ...

If you scroll quickly, watch out for the flickering of the fadeIn and fadeOut

I have a script that fades in an element once the user scrolls more than 145px from the top of the page. $(window).scroll(function(){ if ($(this).scrollTop() > 145) { $('#fademenu').fadeIn(); } else { $('#fademenu').fadeOut(); } } ...

Is it possible to disable a function by clicking on it?

I currently have a website that is utilizing the following plugin: This plugin enables an image to be zoomed in and out through CSS transforms, with default controls for zooming in and out. My goal is to incorporate a reset button that returns the image ...

Dragging objects on a map is done using jQuery and it causes them to bounce

Currently, I am working on implementing the JQuery Draggable feature. My idea is to create a map where I can attach image Divs to it dynamically using Jquery. So far, I have been successful in adding the Divs and making them draggable around the map effici ...

Prevent the page from closing by implementing a solution within the ionViewWillLeave method

I'm looking to use the ionViewWillLeave method to prevent the page from closing and instead display a pop-up confirmation (using toast.service) without altering the form. ionViewWillLeave(){ this.toast.toastError('Do you want to save your cha ...

"Troubleshooting Primeng's p-table filter issue when using an editable FormArray - encountering problems with the

I'm encountering a problem with the filter functionality on primeng p-table when using formarray controls. The filter doesn't seem to work correctly, so I tried registering a custom filter and testing it. However, I found that the value is null f ...

Using jQuery's .html function to insert images

I have a unique counter that counts in currencies, such as Yen and Euro. Each number, as well as the currency sign and separator, are all displayed on the webpage using custom-designed icons. I utilize the display: flex; property in my container div and ap ...

Transferring Information in Angular 8 between components on the Same Level

Currently utilizing Angular 8 with two independent components. One component contains a form with an ID field, and the other is accessed by clicking a button from the first component. The second component does not display simultaneously with the first. A ...

Add content or HTML to a page without changing the structure of the document

UPDATE #2 I found the solution to my question through Google Support, feel free to read my answer below. UPDATE #1 This question leans more towards SEO rather than technical aspects. I will search for an answer elsewhere and share it here once I have th ...

Utilizing an Ajax request to fetch a JSON file via FTP, we must display a progress bar indicating the percentage of

Utilizing jQuery ajax to retrieve a file from an FTP server and wanting to display the loading percentage in a Progress loader. Previously, when using HTTP requests and XMLHttpRequest, the following code was effective: $.ajax({ xhr: function() { ...

How can I ensure a module in Angular module federation v14 is only loaded in either the parent or child app, but not both?

I am currently utilizing Angular 14 along with module federation in my project. Within my child application, I have the following configuration: module.exports = withModuleFederationPlugin({ name: 'childapp', exposes: { './app1&apos ...

Prevent the unwanted 100 Continue behavior in Ajax requests

My Javascript application sends out large POST requests to a backend server. Due to the fact that we need all requests directed to our non-public endpoint and the load balancer/proxies struggle with the Expect: 100-Continue header, I am seeking a way to pr ...

Replace the css variables provided by the Angular library with custom ones

Having an angular library with a defined set of css variables for colors applied to components, which are globally set like this: :root { -color-1: #000000; -color-2: #ffffff; ... } In my application utilizing this library, I need to override ...

Is there a way to go back to the previous URL in Angular 14?

For instance, suppose I have a URL www.mywebsite.com/a/b/c and I wish to redirect it to www.mywebsite.com/a/b I attempted using route.navigate(['..']) but it seems to be outdated and does not result in any action. ...

Browsing a container with JavaScript

I am attempting to display one div at a time and scroll through them repeatedly. I found and modified a Fiddle that works as intended, but when I try to implement it on my own test page, the divs do not scroll as expected. Here is the Fiddle example: http ...

When calling UIComponent.getRouterFor, a TypeScript error is displayed indicating the unsafe return of a value typed as 'any'

I have recently integrated TypeScript into my SAPUI5 project and am encountering issues with the ESLint messages related to types. Consider this simple example: In this snippet of code, I am getting an error message saying "Unsafe return of an any typed ...

Error message: Error in jQuery: Object is required for Internet

I have a button that is designed to trigger the opening of a jQuery UI Dialog when clicked. Strangely, it works perfectly in FF3, FF4, Chrome, and IE8 with ChromeFrame, but fails to function in regular IE8. The error message displayed simply states "Object ...

Implementing AJAX functionality in Codeigniter modulesTo integrate AJAX capabilities within Codeigniter modules

I am currently working on a webpage using the CodeIgniter framework. I am looking to integrate a 'show more' button that will utilize 'ajax.php' to retrieve data from the database and dynamically display it on the site. I prefer for thi ...