user2787474
user2787474

Reputation: 129

How to get checked checkbox and associated text box value in an array and append this value to url using jquery or javascript

In my scenario there is a list of items, check box and quantity field text box is assigned to each item. What i want to achieve is to get all checked check box and associated quantity field text box value in an array and append it to url.

Need to pass these values in below mentioned url

shopping.sandbox.netsuite.com/app/site/backend/additemtocart.nl?c=4030365&n=1&buyid=multi&multi=checkboxvalue,quantityvalue;checkboxvalue,quantityvalue; etc

This is my html code

<a href="" onclick="javascript:addURL(this);alert(this.href);" id="ct1">Add to cart</a> 
<div class="cell-checkbox">
<input type="checkbox" name="select" value="    
<%=getCurrentAttribute('item','internalid')%>" class="big">
</div>
<div class="cell-cart">
<input name="qty" id="qty" value="1" class="input">
</div>          

using <%=getCurrentAttribute('item','internalid')%> i'm getting checkbox value. on clicking add to cart anchor tag below mentioned script is executing.

What i have tried so far

<script>
function addURL(element)
{     
var qtyvalue = document.getElementById("qty").value;
var val = [];  
$(':checkbox:checked').each(function(i)
{          
val[i] = $(this).val();
$('#ct1').attr('href','http://shopping.sandbox.netsuite.com/app/site/backend/additemtocart.nl?c=4030365&n=1&buyid=multi&multi='+val[i]+','+qtyvalue);
});
}
</script>

But its adding only single check box and quantity field value to url not entire array.I want to add these values in format something like this

checkboxvalue,quantityvalue;checkboxvalue,quantityvalue;etc

I am new to java script and jquery so need help to figure this out.

Upvotes: 0

Views: 1754

Answers (3)

Prasad Guduri
Prasad Guduri

Reputation: 125

Generate a string with selected values and append after the loop. Like Below

 function addURL(element)
{     

var selectionString = '';  
$(':checkbox:checked').each(function(i)
{         

selectionString = selectionString + $(this).val()+','+$('.input').eq(i+1).text();

});
$('#ct1').attr('href','http://shopping.sandbox.netsuite.com/app/site/backend/additemtocart.nl?c=4030365&n=1&buyid=multi&multi=selectionString);
}

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82241

You can iterate over checked checkboxes and create new array that have checked value + comma separator + current qtyvalue

and then join them with ; character:

var result = [];
$(':checkbox:checked').each(function(i){
 result.push($(this).val() + "," + $(this).parent().next().find('input').val());
});
var strreuslt = result.join(';'); // checkboxvalue,quantityvalue;checkboxvalue,quantityvalue;etc
$('#ct1').attr('href','http://shopping.sandbox.netsuite.com/app/site/backend/additemtocart.nl?c=4030365&n=1&buyid=multi&multi='+strreuslt);

Upvotes: 1

Satish Kumar sonker
Satish Kumar sonker

Reputation: 1288

You are adding the href attr in each loop that's why its appending the final value of val array in href attr

Try below one

<script>
function addURL(element)
{     
var qtyvalue = document.getElementById("qty").value;
var val = [];  
$(':checkbox:checked').each(function(i)
{          
val[i] = $(this).val();

});
$('#ct1').attr('href','http://shopping.sandbox.netsuite.com/app/site/backend/additemtocart.nl?c=4030365&n=1&buyid=multi&multi='+val.toString()+','+qtyvalue);
}
</script>

Upvotes: 1

Related Questions