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?

Related posts

Leave a Reply

1 comment

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