Passing an object as a function parameter in jQuery
I am trying to pass a JSON element to a function, but it's not working. Any thoughts? Here is my code:
$(document).ready(function () {
$.ajax({
type: "GET",
url: "/smallb开发者_如何学JAVAusiness/_assets/js/events.json",
success: formatJson,
error: function() {
alert("Data file failed to load");
}
});
});
function formatJson (data) {
var json = data.events.event;
var containerList = $('#event-list');
var containerDescrip = $('#event-descrip');
var template = '';
var defaultDescrip = '';
//Format and display event list
$.each(json, function(i, item) {
template += '<p><a href="javascript:void(0)" onClick="formatDescrip(' + i + ',' + json[i].title + ')">' + this.title + '</a></p>';
});
containerList.html(template);
}
function formatDescrip(j, jsonData) {
alert(j);
alert(jsonData);
}
I am trying to pass both i
and json[i].title
to formatDescrip()
but it's throwing this error:
Error: missing ) after argument list
Source File: http://localhost/smallbusiness/webinars.html#
Line: 1, Column: 20
Source Code:
formatDescrip(3,How to Leverage Email Marketing)
What am I doing wrong? And what if I wanted to pass the entire json
object? How would I go about that? It seems like it should be straightforward, but I keep getting errors.
You forgot the quotes around the title.
$.each(json, function(i, item) {
template += '<p><a href="javascript:void(0)" onClick="formatDescrip(' + i + ',\"' + json[i].title + '\")">' + this.title + '</a></p>';
});
Instead of setting up the handlers that way, however, why not use jQuery's ".delegate()"?
$.each(json, function(i, item) {
template += '<p><a class="dynamic" data-index="' + i + '" href="#">' + this.title + '</a></p>';
});
containerList.delegate("a.dynamic", "click", function(ev) {
formatDescrip($(this).data('index'), $(this).text());
});
Or something like that; if the list is extended multiple times then the ".delegate()" call should probably be done once, outside the handler.
edit — if the "formatDescrip()" function needs access to the original "event" object (whatever those things are that you use to make the list of <a>
tags), you can pass it in to "formatDescrip()" instead of the index, and then modify the other function as necessary:
containerList.delegate("a.dynamic", "click", function(ev) {
formatDescrip( json[$(this).data('index')] );
});
// ...
function formatDescrip(eventObj) {
alert(eventObj.title);
//
// ... more code here ...
//
}
I already explained in my comment what is the problem (missing quotes).
But it is better to not create the HTML this way, but to create "real" elements. It is even easier with jQuery:
var $div = $('<div />');
//Format and display event list
$.each(json, function(i, item) {
var title = json[i].title;
$('<p />').append(
$("<a />", {
href: "javascript:void(0)", // you should use a proper URL here
text: title,
click: function(e){
e.preventDefault();
formatDescrip(i, title);
}
})
).appendTo($div);
});
containerList.empty().append($div.children());
Update: Or maybe even better, use .delegate()
as @Pointy suggests.
I think that maybe you have a simple quote as part of the title and that is breaking your code. Try to scape those characters in your string or using double quotes instead simple ones.
精彩评论