Open full view…
Anybody out there willing to help me out with some Javascript/jQuery?
jpesteban
Thu Apr 10 2014 10:28:35 GMT+0000 (UTC)
I'm at my wits end. Admittedly I'm just a beginner at both, but the solution to this is probably very simple; I just can't see it.
I've put up my issue on [Stackoverflow](http://stackoverflow.com/questions/22984019/i-want-to-execute-this-javascript-multiple-times-with-different-targets), but the gist of it is this:
The following code (in its entirety) gets injected into the footer of *each blog entry* in a webpage which is an index of blog entries:
---
<a name="comments-outer-wrapper">
<a class="muut" href="https://muut.com/i/test01293481/comments:test">Comments</a>
[removed]
/* Set the variables for Muut comment title, href, and counter API */
var mtitle = document.title.slice(0, -13);
var mhref = $("article").attr("id").substring(8);
var mcount = "//api.moot.it/postcounts?path=/test01293481/comments:" + mhref;
/* Get the counter size, and prepend the result into blog article entry header */
$.getJSON(mcount, function(json) {
var results = $(".entry-actions");
$.each(json, function(key, val) {
results.prepend("<a >" + val["size"] + " Comments</a>");
});
});
/* Now replace the HREF and TITLE attributes in the original Muut link */
$(".muut").attr( "href","https://muut.com/i/test01293481/comments:" + mhref);
$(".muut").attr( "title",mtitle);
[removed]
---
It works perfectly fine in individual blog article pages. However, on the blog index (where the code is injected in the footer of *each article entry*), it messes up: [Screenshot of page](//res.cloudinary.com/moot/image/upload/t_d3-gallery-s1/v1397125698/:moot:RUxg:screenshot20140410at4.31.59pm.png.jpg)
You can see how it'll prepend a comment counter entry 3 times (once for each blog entry currently in the index), and it does this for each entry. Additionally, the counter links to the same URL, and only gets the comment count result from that URL.
Anyone have any suggestions? I've searched for a solution for a couple days now, to no avail.
@albertovasquez
2014-04-10T10:28:35.000Z
One thing you could do is only do the prepend if that element doesn't already exist. So a if ($(".entry-comments").length > 0) skip .. That might address your issue of duplicate entries.
@jpesteban
2014-04-10T10:28:35.000Z
It might. But it won't address the issue of the actual HREF for all the entries being the same. The variables aren't being refreshed between instances of the script.
@courtneycouch
2014-04-10T10:28:35.000Z
I'll ping tero about this.
@jpesteban
2014-04-10T10:28:35.000Z
Thanks @courtneycouch, I didn't want this to get in the way of Muut development but I do appreciate all the help I can get. If you or @tipiirai would like to see the site in question, it's [here](http://gamebros.squarespace.com/blog/). You'll have to view it as a Visitor and enter the captcha since it's still a Squarespace trial site.
@tipiirai
2014-04-10T10:28:35.000Z
First of all I like the fact that you are using class="muut" instead of class="moot", haha. Plsease replace api.moot.it with api.muut.com too.
@jpesteban
2014-04-10T10:28:35.000Z
Thanks so much for taking a look, Tero. I'll scour the code in that demo site once I get to work, as that solution to multiple instances of Muut on a single page looks nice.
@tipiirai
2014-04-10T10:28:35.000Z
I think it will since the `prepend` method is called multiple times because of multiple return values of the /postcounts call.