0

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...

1 Answer 1

1

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);
    }
  }
}

Sign up to request clarification or add additional context in comments.

1 Comment

sorry to say that it is not in form these are just checkboxes displaying in a loop.

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.