1

I have a form where user can fill details and on clicking of add attendee button can add up to 9 people details. I am getting value of user name, selected checkbox item id and item quantity and storing inside array and passing these values in url to add selected items and created user On first click but when i click second or third or multiple times its adding duplicate value inside array. I want to check if values exist or not if its there need to increase quantity and doesn't want to add it again in array.

Here is my code

 jQuery(document).ready(function() {
     var allVals = [];
     jQuery("#btnadd2").click(function() { //Clicking on this add attendee button getting values
         var nameatt = jQuery("#txtAttendeeNames").val();
         var qty = jQuery('input[name="qty"]').val(); //increase quantity if selected checkbox id is already present in array   
         jQuery(".big:checked").each(function() {
             allVals.push($(this).val()); //getting selected checkbox value and based on this getting item id which should not exist in array
         });
         for (i = 0; i < allVals.length; i++) { //creating url and updating multi div with this
             var rslt = allVals.join(','
                 '+'
                 custcol_name_of_attendees | '+nameatt+' | +';');
             rslt = jQuery('#multi').val(rslt);
         }
     });
 });

1 Answer 1

2

Use jQuery.inArray() in jquery. The jQuery.inArray() method is similar to JavaScript's native .indexOf() method in that it returns -1 when it doesn't find a match. If the first element within the array matches value, jQuery.inArray() returns 0.

jQuery(".big:checked").each(function() {
   if(jQuery.inArray($(this).val(), allVals) == -1) {
     allVals.push($(this).val()); 
   } else {
     // Already present in the array
   }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response...to increase quantity if its already present in array where should i place the code?
I have added code to increase quantity but its not working its showing 1 not 2 for duplicate id....this is my code
jQuery("#btnadd2").click(function(){ var qty = 1;var allVals = [];var nameatt= jQuery("#txtAttendeeNames").val(); jQuery(".big:checked").each(function(){ var qty =parseInt( jQuery(this).parents().nextAll().children().find('input[name="quantity"]').val()); if(jQuery.inArray( jQuery(this).val(), allVals) == -1) { allVals.push( jQuery(this).val()); } else{ qty = parseInt( jQuery(this).parents().nextAll().children().find('input[name="quantity"]').val());+1;}}); for(i=0;i<allVals.length;i++){ var rslt = allVals.join(','+qty+','+'custcol_name_of_attendees|'+nameatt+'';');}});

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.