Events defined in $.each (creating list elements with events) not executing
Hello,
I am trying to "ajaxify" my site. Now I have one problem:
- $("#posts").children().remove();
- $("#tag-sidebar").children().remove();
- $.each(data.Tags_Sidebar, function (indexInArray, valueOfElement) {
- var insert = $("<li>");
- insert.addClass(valueOfElement.Type);
- var href1 = $("<a>");
- href1.attr("href", "#");
- href1.bind("click", {valuee: valueOfElement }, function(event) {
- //document.location = "/Post/List/1/" + data.Tags + "+" + valueOfElement.Name;
- alert("ahh");
- $("#tags").attr("value", $("#tags").attr("value") + " +" + event.data.valuee.Name);
- event.stopPropagation();
- return false;
- });
- href1.html("+");
- var href2 = $("<a>");
- href2.attr("href", "#");
- href2.bind("click", {valuee: valueOfElement }, function(event) {
- //document.location = "/Post/List/1/" + data.Tags + "+-" + valueOfElement.Name;
- $("#tags").attr("value", $("#tags").attr("value") + " +-" + event.data.valuee.Name);
- event.stopPropagation();
- return false;
- });
- href2.html("-");
- var href3 = $("<a>");
- href3.attr("href", "#");
- //href3.attr("href", "/Post/List/1/" + valueOfElement.Name);
- href3.bind("click", {valuee: valueOfElement }, function(event) {
- //document.location = "/Post/List/1/" + data.Tags + "+-" + valueOfElement.Name;
- $("#tags").attr("value", event.data.valuee.Name);
- event.stopPropagation();
- return false;
- });
- href3.html(valueOfElement.Name);
- var span = $("<span>");
- span.css("color", "#a0a0a0");
- span.html(valueOfElement.Count);
- href1.appendTo(insert);
- insert.html(insert.html() + " ");
- href2.appendTo(insert);
- insert.html(insert.html() + " ");
- href3.appendTo(insert);
- insert.html(insert.html() + " ");
- span.appendTo(insert);
- insert.appendTo($("#tag-sidebar"));
Now when I click one of those links (href1, href2, href3) generated, the click event won't execute! What's the problem? Also, is it right that I have to transfer the valueOfElement over, like I did?
What does stopEventPropagation do? Prevent the href from being navigated to? That's what I am trying to do.
The data object is JSON fed from here: http://aspbooru.tk/Post/List/?json=1
The HTML is here: http://aspbooru.tk/Post/ListAjax