I need to create a array which needs to have custom index and value in jquery each function. For eg. i need to create an array like this [4: 'sampleIndex1','test':'sampleIndex2'];
But while using each function jquery takes '4' index will be an 4th index of an array remaining index 0,1,2,3 will set an value to empty. I dont need 0 1 2 3 index in the array. Instant help is much appreciated!!!
Thanks in advance,
Below are the sample code, I need to create div elements for select options.
<select name="question" id="myselect">
<option value="sample1">What is your father's name?</option>
<option value="sample2">What is your pets's name?</option>
<option value="sample3">What is your school's name?</option>
</select>
$("#myselect").changeselect({width:400});
$.fn.changeselect=function(options){
var defaults = {
width: 600
};
var options = $.extend(defaults, options);
var createhtml="";
var selectarr=new Array();
$("option",this).each(function(k,v){
selectarr[$(this).attr('value')] = $(this).html();
createhtml +="<div id='"+k+"'>"+$(this).html()+"</div>";
});
console.log(selectarr);
$(this).after(createhtml);
};
it shows [] empty array Its need to be like this [{"sample1":"What is your father's name?", "sample2":"What is your pets's name?", "sample3":"What is your school's name?"}]