Ashley Strout
Ashley Strout

Reputation: 6268

jQuery click() fails with "Could not convert JavaScript argument"

Here's the setup: A user fills out a certain form on my page, which is submitted via AJAX (jQuery $.post). In the return function I add an element to the page related to the form that was submitted. This element has a click() handler attached to it. The element goes on the page without issue, but when I click it, I get the dreaded "Could not convert JavaScript argument". Here is the line that adds the element to the page:

$('<span class="appointment" id="a'+aid+'">'
    +type+name+'</span>').click(appointmentDetails).appendTo(calSlot);

Again, this is being called inside the success callback of the jQuery post function. The appointmentDetails function exists, as do all the variables. I also tried like this:

var newHTML='<span class="appointment" id="a'+aid+'">'+type+name+'</span>';
$(calSlot).append(newHTML);
$("#a"+aid).click(appointmentDetails);

The appointmentDetails function uses either a passed ID or this.id to get the appointment ID, then runs a jQuery .post to get and display the appointment details. Here are the basics:

function appointmentDetails(appID) {
    if (!appID) var appID=$(this).attr("id").substr(1);
    $.post("data/appointments.php", {aid: appID, action: "details"}, function(data) {
        //Callback stuff. Doubtful it's relevant
    }, "xml");
}

Upvotes: 0

Views: 59

Answers (1)

epascarello
epascarello

Reputation: 207557

function appointmentDetails(appID) <--

appID would be a jQuery event object!

It is not undefined/false like you are expecting. You need to check if it is an object/string if you are overloading this function.

Upvotes: 1

Related Questions