Raheel
Raheel

Reputation: 177

get checked check boxes and store in array javascript

anyone can tell me how can i check and add checkbox checked values into an array?? get checked values on click and store in an array in javascript. This is my code.

function ajax(city)
{
var xmlhttp; 
try { 
    xmlhttp=new XMLHttpRequest(); 
} 
catch (e) { 
    try { 
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
    } 
      catch (e) { 
        try { 
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
        catch (e) { 
            alert("Your browser does not support AJAX!"); 
            return false; 
        } 
    } 
} 

var url='process.php?city='+city ;
xmlhttp.onreadystatechange=function() { 
    if(xmlhttp.readyState==4) 
        document.getElementById('search_city_projects').innerHTML = xmlhttp.responseText;
}
xmlhttp.open("GET",url,true); 
xmlhttp.send(null); 
}

Here is html code..

<input type="checkbox" name="checkbox[]" id="checkbox[]" value="<?php echo $cities[$i]->city; ?>" onclick="ajax('<?php echo $cities[$i]->city; ?>');" />

i don't how to add checkbox values that are gets from on click event and then store these values in array one by one in a single array and then pass these array's values to ajax call.Please help... Thanx in advance...

Upvotes: 0

Views: 1893

Answers (1)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

Do you mean:


var chkedVals = new Array; //array to store checked checkboxes value
with(document.your_form_name) {
  for(var i = 0; i < checkbox.length; i++){
    if(checkbox[i].checked) {
       chkedVals.push(checkbox[i].value);
    }
  }
}

Upvotes: 1

Related Questions