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

Retrieving the initial response text using jQuery's ajax function

Is there a way to retrieve the original text of an AJAX request? For example, if I specify contentType: 'application/json', can I receive a string in the success method instead of a parsed JSON result? (Unfortunately, I am unable to set the Conte ...

What is the process of adding new fields to a model in TypeScript?

I have created a test.model.ts file: export interface ITransaction { description: string; transactionDate: string; isDebit: boolean; amount: number; debitAmount: string; creditAmount: string; } export class Transaction implements ...

Is it better to choose AJAX and JQuery for their speed and complexity, or stick with the simplicity of GET requests?

When a user attempts to log in and fails, an error message should be displayed on the page. There are two primary methods that can be used to achieve this: one involves using a form action on the HTML page and handling incorrect login information in the PH ...

Ajax encountering 404 error in ASP MVC3 website

When testing this Ajax code on my local machine, it works perfectly. However, once we deploy it to a DEV server, we encounter a 404 error. The website is built on ASP MVC3 and interacts with a SQL database without any issues elsewhere. I am new to Ajax and ...

"How can I exclude empty fields from the form.serialize function in jQuery when using php?" is not functioning as expected

I am facing an issue with using How do I use jQuery's form.serialize but exclude empty fields. Despite following a minimal example that directs to a php script: <html><head> <script src="jquery.min.js"></script> <script typ ...

What causes an error during the compilation of an Angular package containing a singleton class?

I am currently in the process of creating an Angular library. Within this library, I have developed a singleton class to manage the same SignalR connection. Here is the code implementation: import * as signalR from '@microsoft/signalr'; export c ...

Utilize jQuery's validate function to compare and assess two different time values

I am working on a project where I have two select dropdowns, one for selecting a time from and the other for selecting a time to. The options in these selects are in intervals of fifteen minutes. I use PHP's strtotime function to convert these selecte ...

Accessing the element reference within a custom attribute directive in Angular through a projected template using ngTemplateOutlet

I'm encountering an issue when trying to adjust the css and click event on a custom attribute directive The DIV causing the problem is nested within a reference that is projected and passed into a child component. Here is the process: Add the ng-te ...

When working with Angular 8, you may encounter an issue where the AWSIoTProvider from aws-amplify

After referencing the official link document here, I found that my code works fine with ng serve. However, after building it and trying to access the page, I encountered an error stating "AWSIoTProvider is not a constructor ". Despite searching for a sol ...

What is the proper way to bind data next to a string in Angular?

I am facing an issue with two variables that are supposed to form a link for an image. I have tried putting them in the src tag using the following code snippet: [src] = {{part1}}+"_"+{{part2}} However, this approach is not working as expected. The scr ...

Troubleshooting a problem with jQuery child items

Could someone help me understand why the second div is affected by the last part of the code and not the first? It's puzzling to see all the content disappear, especially when I expected the first div to be impacted as it seems like the immediate pare ...

Is there a way to declare the different types of var id along with its properties in Typescript?

I recently received a task to convert a JavaScript file to a TypeScript file. One issue I am currently facing is whether or not I should define types for the 'id' with this expression, e.g., id={id}. So far, I have tried: Even though I defined ...

Setting the Node.js version in Eclipse on a Windows operating system can be easily accomplished by following these

After attempting to execute "ng serve" within Eclipse, an error message is displayed: The error states that you are currently using Node.js version v6.9.4, which is not compatible with Angular CLI 8.0+. To successfully use Angular CLI, you need to have No ...

Struggling to insert a span element into an anchor tag within a dynamic WordPress navigation menu

I am currently attempting to insert a span tag within a tags in the secondary navigation located on the second level of the following page URL. Thus far, I have only discovered methods for adding span tags to the main navigation as well as child and grand ...

Transferring files to Django using AJAX

I am struggling with the process of uploading files to django using ajax. The upload is done within a modal window. The Form <div class="modal fade bs-example-modal-lg" id="fileUploadModal" role="dialog" aria-hidden="true"> <div class="modal ...

Implementing the Infinite Scroll feature with 'react-infinite-scroll-component' on local JSON data: A step-by-step guide

Currently experimenting with frontEnd development, I have incorporated the 'react-infinite-scroll-component' as a dependency. My goal is to apply this effect to my local Json data without relying on any API calls at this stage. I'm encounter ...

Troubleshooting: Why Your Long Polling Example Isn't Working in Just 5 Minutes

Can anyone spot any errors in this code? Check out the example here I attempted to replicate the example, but it's not functioning correctly. I am looking to create real-time notifications from a database. If anyone can provide tutorials or assistan ...

Exploring the Combination of Laravel 5.1 with Typeahead.js

I am facing an issue with implementing typeahed.js in my Laravel 5.1 blade view. I'm following The Basics example for typeahed but the states are not showing up when I try to search for them. Here is the code for my main layout: <html> <h ...

Dynamic Object properties are not included in type inference for Object.fromEntries()

Hey there, I've been experimenting with dynamically generating styles using material UI's makeStyles(). However, I've run into an issue where the type isn't being correctly inferred when I use Object.fromEntries. import * as React from ...

Trouble arises when attempting to generate maps using leaflet.js with data sourced from a local directory

I've been working on an application that requires a map to function offline. I followed a tutorial, which you can find here: To create the necessary files, I generated an mbtiles file of my desired area at this link: The mbtiles file was saved as &a ...