0

I want to append data to a jquery Modal through onclick and passing array into a function is creating problem. My code is as follows:-

var ttlpndordsqnty = topBarData['ttlpndngordrs'].length;
var ttlpndordsdata = topBarData['ttlpndngordrs'];
$('#totalpendingtestorders').empty();
$('#totalpendingtestorders').append('<a onClick="populateWidgetInfo(\''+ttlpndordsdata+ '\')" class="count red" data-toggle="modal" data-target="#totalpndgordModal"><label >' +ttlpndordsqnty+ '</label></a>');

function populateWidgetInfo(data)
{
    console.log(data);

} //end function

when i click on this then the following is returned instead of array passed. Kindly help me?

[object Object],[object Object],[object Object]
4
  • what does console.log(ttlpndordsdata ) give? Commented Dec 24, 2016 at 20:03
  • This is an array of 3 objects. What's your point Commented Dec 24, 2016 at 20:05
  • 2
    In this line $('#totalpendingtestorders').append('<a onClick="populateWidgetInfo(\''+ttlpndordsdata+ '\')" class="count red" data-toggle="modal" data-target="#totalpndgordModal"><label >' +ttlpndordsqnty+ '</label></a>'); your ttlpndordsdata converts to string and passed to your function once you click. That's why your log is [object Object],[object Object],[object Object] Commented Dec 24, 2016 at 20:06
  • You best create a proper DOM a tag object and assign it's onClick method properly through JS and then use .appendChild() to append it to it's parent. Commented Dec 24, 2016 at 20:08

1 Answer 1

2

ttlpndordsdata is being converted to a string at attribute event handler parameter. Use .on()

$('#totalpendingtestorders')
.on("click", "a.count", function() {
  opulateWidgetInfo(ttlpndordsdata)
})
Sign up to request clarification or add additional context in comments.

2 Comments

I was going to answer this one :)
@Karlen Why can you not still post an Answer? Your interpretation of Question, explanation and approach could possibly contain greater detail and description of issue and solution than present Answer; and potentially be useful to OP and SO.

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.