3

sorry for my bad english

i am working in javascript and i have build an array i want to enter values in this array.

var attribute_sets = [];
$('.attribute_set :selected').each(function(i, selected){
    attribute_sets[i]['id'] = $(selected).val(); // getting id 
    attribute_sets[i]['name'] = $(selected).text(); // getting name
});

Its giving me error

TypeError: attribute_sets[i] is undefined

also tried this one

attribute_sets[i]['id'].push($wk_jq(selected).val());

still getting same error

can any one please guide me how can i insert values in this JS array. i want output like this

array
    [0]
      'id':'1',
      'name':'abc'
    [1]
      'id':'2',
      'name':'xyz'
2
  • What type of elements are you trying to get? Commented Jun 1, 2016 at 14:03
  • i am trying to id and name from multi select and i am sending this array to php Commented Jun 1, 2016 at 14:04

2 Answers 2

2

try

$('.attribute_set :selected').each(function(i, selected){
  attribute_sets.push({
   id: $(selected).val(), // getting id 
   name: $(selected).text() // getting name
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much works like a charm... :) Thanks alot
2

Use map() function.

attribute_sets = $('.attribute_set :selected').map(function(i, selected){
    return {
       'id' : $(this).val(),
       'name' : $(this).text(),
    }
}).get();

1 Comment

Thank you so much works like a charm... :) Thanks alot

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.