2

I have anchor text that can look like "1 Reply and 30 Mentions" or "3 Replies and 1 Mention" (obviously with varying numbers). I'm trying to use jQuery to replace that anchor text with the added number.

$(document).ready(function() {
$('header div.comments-link a').each(function(index) {
    var myreaction = $(this).text().split(" ");
    $(this).text(myreaction[0]+myreaction[3]);
});
});

So for the first example, it would be 1+30 so the text replacement would be 31. For the second example, it would be 3+1 so the text replacement would be 4.

However, it doesn't appear to be replacing the actual text and doesn't seem to be doing the split array properly. As it's going down the page, it's sometimes grabbing "1" but other times grabbing "Reply".

Any ideas?

1
  • Could you show us some HTML or make a jsfiddle? Commented Jul 15, 2012 at 4:25

3 Answers 3

3

I'd try a regex and parsing the number strings into actual integers:

var myreaction = $(this).text().match(/(\d+) Repl(?:y|ies) and (\d+) Mention(?:s?)/);
$(this).text(parseInt(myreaction[1], 10) + parseInt(myreaction[2], 10));
Sign up to request clarification or add additional context in comments.

5 Comments

This doesn't match "Replies" or "Mention".
Proof that it works (slightly modified to make the fiddle easy): jsfiddle.net/pGH6p Upvoted, but as someone who personally still hasn't become fluent in regex and knows other developers who also are not, it looks like Greek to me. Knowing I'm not alone makes me wonder about regex as a maintainable solution.
You'll figure it out sooner or later. It's much more readable than split() and scales really well for more complex things. Truthfully, I would be looking for the source of these values, as they are dynamically generated, instead of extracting them from text.
Yes, I agree, that's the first thing I thought, too. Go right to the source! I'll keep poking away at regex until I can read it... ;) The thing the split has going for it is that you can modify the code (see my fiddle in the comment) such that you can change the phrase and it doesn't matter.
It just takes a few self-initiated research marathons. I've been looking at regex with a disgusted face for about two years until I realized that it does some magical stuff (which I, being stubborn, only found while playing with personal projects).
2

working demo http://jsfiddle.net/BxUXX/1/

For adding the 2 use parseInt

hope it helps

code

var str = "1 Reply and 30 Mentions";

$(document).ready(function() {
   // $('header div.comments-link a').each(function(index) {
        var myreaction = str.split(" ");
        alert(parseInt(myreaction[0]) + parseInt(myreaction[3]));
   // });
});​

4 Comments

As a giggle (read as: I didn't really optimize code) I took this idea and expanded on it so that you don't have to look for specific indexes of myreaction: jsfiddle.net/BxUXX/3
@GregPettit ha ha ha ha - lol :) Are you in ICFP 2012 - man its getting hotter in it! I will keep your jsfiddle to share across :)
No, I am not. Be sure to keep your shirt on. ;)
@GregPettit heh we all will try lol :) see you around bruv!
0

try this:

$(document).ready(function() {
  $('header div.comments-link a').each(function(index) {
    var s = $(this).text().replace(/\D/g, "").split("")
    $(this).text(parseInt(s[0], 10) + parseInt(s[1], 10));
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.