0

I have a list of checkboxes for CITY (class='city') . Now I am creating an array which store all the city checked values. my code is:

$('.city:checked').each(function() {
    city.push(this.value);
});

alert(city);

Result: mumbai,delhi

But I want result like 'mumbai','delhi' in array. please suggest,

4
  • can you add html also Commented Oct 2, 2014 at 7:11
  • Actually it array only. But when you do an alert toString() function is called on it which will display it comma separated. Commented Oct 2, 2014 at 7:12
  • only check-boxes are there in my HTML. Commented Oct 2, 2014 at 7:13
  • I want all values inside "" Commented Oct 2, 2014 at 7:14

3 Answers 3

1

try this:-

var city=[];
$('.city:checked').each(function() {
   city.push("'"+this.value+"'");
});
alert(city);

Demo

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

1 Comment

Thanks mohit. It works. Sorry I can not vote up as i have only 14 reputation .minimum 15 requires :(
0

once you get the result split the result. check below code sample:

var res = city.split(',');

1 Comment

I dont want to split .result should be 'mumbai','delhi' not mumbai,delhi
0

it shows like that in alert but in actual they are array:-

$(document).ready(function(){

   var someGlobalArray = new Array;


someGlobalArray=[];
$('.city:checked').each(function() {
    someGlobalArray.push("'" + $(this).val() + "'");
});
alert(someGlobalArray);    
});

Demo :-

http://jsfiddle.net/PBhHK/412/

Comments

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.