Hi Friends,
I want to pass Array or Object as parameter in function
But can't, Here is my code
Please help me,

var InfoArray = new Array();
for(i=0;i<5;i++)
{
   InfoArray['name'] = "ABC";
   InfoArray['id'] = "A123";
   j = i+1;
   $('#selectorName').after("<input type='text' id='txt"+j+"+
   "onFocus=prePopulate('txt"+j+"','"+InfoArray+"','Hello','Hi');>");
}

And Function is :

function prePopulate(txtName,myArray,path,msg)
{
   alert(myArray['name']); // Shows me undefined..!!!
   // .....
   // .....

   // .....
   // .....
}

Please Help me and thx in advance.

pThu,

You will find it easier to build the input element in a more jQuery way. There's no unique solution but something like this should work:

...
$input = $('<input type="text">').attr('id','txt'+j).focus(function(){
	prePopulate(this.id, InfoArray, 'Hello', 'Hi');
});
$('#selectorName').after($input);
...

You may want to pass this to prePopulate() rather than this.id as this will make all its properties available, including this.id .

You may run into difficulties with path and msg arguments if they are defined in the loop. Done in the wrong way, all iterations will acquire the values of the last iteration. There are two standard solutions:

  • establish one closure per iteration of the loop (tricky until you get used to it)
  • use jQuery.data() to associate data with its DOM element.

Carry on with what you are doing and you will probably see what I mean.

Airshow

If, as it seems, InfoArray has been defined in the global scope, just use it directly in the function. There's scant difference between passing a reference-to-an-array to a function and accessing the array directly within the function (the CPU cycles needed to push the reference to the array onto the stack); either method works with the data in the outer scope.

But to address your direct problem, '"+InfoArray+"', erase the single quotes. With them, the function call looks like prePopulate(..., 'InfoArray', ...). In other words, you are passing a string to the function instead of a reference to the intended array.

Use firefox and the firebug JS debuggerer (or equivalents for other browsers) so you can inspect the actual changes you make to the HTML. While developing a 10k line JS program, I found firebug to be indispensable.

var InfoArray = new Array();
for(i=0;i<5;i++)
{
   InfoArray['name'] = "ABC";
   InfoArray['id'] = "A123";
   j = i+1;
$('#selectorName').after(
"<input type='text' id='txt"+j+"+"onFocus=prePopulate('txt"+j+"','"+InfoArray+"',(!and it all gets messed up)Hello','Hi');>");}

1.
You have a "type mismatch" there - Arrays cant have Named Properties.
But that shouldn't be a problem because Array is an Object to, and all constructed objects can receive named properties, including the obscure and rarely used: Boolean object. -Meaning it is not the cause of failure.

function prePopulate(txtName,myArray,path,msg)
{
   alert(myArray['name']); // Shows me undefined..!!!
   // .....
}

3.
That is namely a Reference Error end result inherited from a preceding chain of errors in a malformed string.
Your "myArray" will not return 'reference error' but 'undefined', because it's a declared argument name, which never received a value. To correct this you need to go back to the cause.

2.
The correct(ed) string in your jQ statement would be:

$('#selectorName').after(
"<input type=text id=txt"+j+" onFocus=prePopulate(txt"+j+","+InfoArray+",'Hello','Hi')>"
)}

Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.