0

Here is my requirement, I have an array having datewise data like

$.each(dateWiseArr, function(index, currentDate){
    myDateArray[currentDate] = "myData";
    dateWiseOtherId[currentDate] = "otherId";
});
alert(myDateArray+"::"+dateWiseOtherId);//working fine and showing right data
$("#myBtn").attr("onClick", "myPassingFunction("+myDateArray+", "+dateWiseOtherId+")");

function myPassingFunction(myDateArray, dateWiseOtherId){
    alert(myDateArray[0]);//throw undefined
}

Now when I am press my button function call successfully but variables becomes string.

Let me know if you need more understanding.

Note: myDate

7
  • 4
    You're using an odd way of binding a click event. Look into a basic jQuery event tutorial - that will also solve your string issue. Commented Nov 7, 2012 at 12:42
  • that is not how you suppose to register listener in jQuery. Please have a look at api.jquery.com/click Commented Nov 7, 2012 at 12:43
  • I cannot bind click event onload as parameters and function are change as per condition Commented Nov 7, 2012 at 12:44
  • you might also want to use console.log (not in IE!) instead of nasty alert boxes Commented Nov 7, 2012 at 12:44
  • 1
    the reason this is not working for you is becouse you really are passing only the string representation of the array when you concat the onclick value like this. Commented Nov 7, 2012 at 12:44

3 Answers 3

1

Bind a function instead of binding a string, then you don't need to convert the values to strings:

$("#myBtn").click(function(){
  myPassingFunction(myDateArray, dateWiseOtherId);
});
Sign up to request clarification or add additional context in comments.

Comments

1

Try below. It should work.

$.each(dateWiseArr, function(index, currentDate){
    myDateArray[currentDate] = "myData";
    dateWiseOtherId[currentDate] = "otherId";
});

alert(myDateArray+"::"+dateWiseOtherId);//working fine and showing right data

$("#myBtn").click(function (event) {
     myPassingFunction(myDateArray, dateWiseOtherId);
});

function myPassingFunction(myDateArray, dateWiseOtherId){
    alert(myDateArray[0]);//throw undefined
}

Comments

0

Right way of binding events: Check this working fiddle http://jsfiddle.net/vC4Yk/

var myDateArray = ["some data"];
var dateWiseOtherId = ["some other data"];

alert(myDateArray+"::"+dateWiseOtherId);//working fine and showing right data

$("#myBtn").click(function(){
   myPassingFunction(myDateArray, dateWiseOtherId);      
});

function myPassingFunction(myDateArray, dateWiseOtherId){
    alert(myDateArray[0]);
}   ​

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.