yoram yoram - 3 years ago 805
HTML Question

add function onclick in json parse

Im trying to load article from json (its works) and add it link to fucntion, but when I add the a href tag with onclick function (for example - alert), the function doesn't work.

what am I missing?

$.getJSON(categoryAddr,function(data){
json=data;
jQuery(function ($) {
$.each(json, function (i, value) {
var list ="<a href='#' onclick='alert('hey!');'>"
+"<h1>" + value.title + "</h1>"
+ "<img src='" + value.image + "' alt=''/>"
+"<h2>" + value.excerpt + "</h2></a>";
$('.hold').append(list);
});

});

});

rzr rzr
Answer Source

Use escaped double quotes around your JS

var list ="<a href='#' onclick=\"alert('hey!');\">"

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download