Nisarg Bhavsar
Nisarg Bhavsar

Reputation: 956

create an array and stored selected value as key and value

I am working in wordpress. And I have created one multiple selected dropdown. My jQuery code is like this:

          jQuery('#int_btn').click(function(){
                //var int_val = "";
                var int_val = [];
                var int_text = [];
                var selected = [];

                jQuery( ".tole_int option:selected" ).each(function() {
                    //int_val += jQuery( this ).text() + " ";
                    int_val.push(this.value);
                    int_text.push(this.text);

                });
                //jQuery( ".int_div" ).text( int_val );

                jQuery.ajax({
                    url:"<?php echo site_url().'/interests-with-stars'; ?>",
                    data:{int_val: int_val,text:int_text}
                    }).done(function(data){
                        //jQuery('.int_div').html(data);
                        });

                });

Now,when I click on button then I have to stored selected value in an array as key and value.Morevover, i want to pass that array in post.So what jQuery I have to write?

Upvotes: 2

Views: 86

Answers (1)

Norlihazmey Ghazali
Norlihazmey Ghazali

Reputation: 9060

Better you use this way, try this :

 var obj = {};
 jQuery( ".tole_int option:selected" ).each(function(i,e) {
              obj[e.value] = e.innerHTML; 
 });

Ajax data should be:

 data:obj,

DEMO - see console

Upvotes: 1

Related Questions