1

I'm looking for some tutorial to make an array data in custom jQuery Function, but I can't find any. Can you tell me how to make an array in jQuery Function? I want to call my function like this :

$(this).myPlugin({ 
    data_first: '1'
    data_second: 'Hello'
});

my function script

(function($) {
    $.fn.myPlugin = function(data) {
       return this.each(function() {
          alert(data[data_first]+' bla bla '+ data[data_second]);
       });
    }
})(jQuery);

2 Answers 2

1

You will wan't this.

alert(data.data_first+' bla bla '+ data.data_second); 

Then you won't be trying to pass a variable

That is a Object you're using not an Array

You can get values from a Object like this

data.data_first; // By string name thing

data['data_first']; // By String Name

var index = 'data_first';
data[index]; // By variable
Sign up to request clarification or add additional context in comments.

Comments

0

To pass the array use:

$.fn.myPlugin = function(array) {
   console.log(array[0] + array[1]);

};

and call

$(this).myPlugin(['1', 'Hello']);

just like normal function

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.