Latest topics
» Auto-suggest @mentions as You Type
by skouliki Today at 04:57

» Hello everyone this is Adam from France
by SLGray Today at 01:09

» FM Themes
by SLGray Today at 01:08

» User - Made Creations
by SLGray Today at 01:07

» Search box only visible on hover
by SLGray Today at 00:58

» Browsers & Updates
by SLGray Today at 00:56

» Change newposts/no new posts/ locked icons
by T.C. Yesterday at 19:52

» [HELP] About Profile Cover Code
by MaHwOs Sun 19 Nov 2017, 18:54

» bloquer un membre
by berlingo Fri 17 Nov 2017, 18:19

» IconSkouliki Graphics
by skouliki Fri 17 Nov 2017, 03:54

» [IN PROGRESS] Can this code work on pun version
by skouliki Thu 16 Nov 2017, 03:50

» [TUTORIAL] [ALL VERSIONS] Warning before posting
by demon99 Wed 15 Nov 2017, 08:12

» About Add Reply & New Topic button (popup form)
by omarpop23 Mon 13 Nov 2017, 20:31

» Add a login popup for the toolbar
by Ape Sun 12 Nov 2017, 18:41

» Post a message automatically when locking a topic
by ThunderTB Sun 12 Nov 2017, 13:51

Recent Tutorials
Who is online?
In total there are 7 users online :: 0 Registered, 0 Hidden and 7 Guests :: 2 Bots

None

[ View the whole list ]


Most users ever online was 172 on Fri 19 Feb 2016, 05:17

Adding text to reputation counter

View previous topic View next topic Go down

avatar
Laura
New Member
Title :
Gender : Unspecified
Posts : 6
Points : 269
Reputation : 4
Language : Spanish, Catalan, English
Browser : Browser : Google Chrome Forum Version : Forum Version : Forumactif Edge
Awards :
View all awards
View user profile

PostLaura on Wed 22 Mar 2017, 05:30

Hi!

First, I'm not too sure this was the right place to post, my apologies if this topic shouldn't go here. Embarassed

What I'm trying to do is add a text to the reputation counter, so that it looks like this before voting:



And like this once you have:



(I have removed the thumbs down option as well as the vote bar with CSS).

I tried just adding the string to the JS, like this:

Code:

$(function(){
  for (var vote = $('.vote'), i = 0, j = vote.length, bar, total, percent, n_pos, n_neg; i < j; i++) {
    bar = $('.vote-bar', vote[i])[0];

    if (bar) {
      total = +bar.title.replace(/.*?\((\d+).*/, '$1');
      percent = +bar.title.replace(/.*?(\d+)%.*/, '$1');

      n_pos = Math.round(total * (percent / 100));
      n_neg = total - n_pos;
    } else {
      n_pos = 0;
      n_neg = 0;
    }

    vote[i].insertAdjacentHTML('afterbegin', '<span class="vote_num vote_good">' + n_pos + ' me gusta ' + '</span>');
    vote[i].insertAdjacentHTML('beforeend', '<span class="vote_num vote_bad">' + n_neg + '</span>');

    $('a', vote[i]).click(function() {
      var that = this,
          links = $('a', that.parentNode);

      $.get(that.href, function() {
        var n = that[/minus/.test(that.className) ? 'nextSibling' : 'previousSibling'];
        n.innerHTML = ++n.innerHTML;
        links.fadeOut();
      });


      links.attr('href', '#').css({
        opacity : 0.4,
        cursor : 'default'
      });

      return false;
    });
  }
});

But it seems it's not enough, as right now it does display the text, but the vote counter doesn't update properly. Once it's clicked, instead of showing the counter with the text, NaN appears (refreshing the page, though, makes the number of votes and text display correctly again).



Could anyone help me out with this, please? Thanks in advance! Smile


Last edited by Laura on Thu 23 Mar 2017, 16:55; edited 1 time in total
avatar
Ange Tuteur
Administrator
Title :
Coffee Addict
Gender : Male
Age : 22
Posts : 4712
Points : 9587
Reputation : 2341
Location : Macungie, PA
Language : EN10, FR5
Browser : Browser : Brave Forum Version : Forum Version : Forumactif Edge
Awards :
View all awards
View user profile https://github.com/SethClydesdale https://twitter.com/sethc1995

PostAnge Tuteur on Wed 22 Mar 2017, 17:14

Hi @Laura,

It's displaying NaN ( Not a Number ), because when the script adds the votes to .vote_num.vote_good it does some math on the innerHTML content, and since there's non-numerical text inside the element it returns NaN. Here's a quick example :
Code:
+'Hello' == NaN // Hello cannot be converted to a number
+'1' == 1 // the string is converted to a number

Here's the corrected script :
Code:
 
$(function(){
  for (var vote = $('.vote'), i = 0, j = vote.length, bar, total, percent, n_pos, n_neg; i < j; i++) {
    bar = $('.vote-bar', vote[i])[0];
 
    if (bar) {
      total = +bar.title.replace(/.*?\((\d+).*/, '$1');
      percent = +bar.title.replace(/.*?(\d+)%.*/, '$1');
 
      n_pos = Math.round(total * (percent / 100));
      n_neg = total - n_pos;
    } else {
      n_pos = 0;
      n_neg = 0;
    }
 
    vote[i].insertAdjacentHTML('afterbegin', '<span class="vote_num vote_good">' + n_pos + '</span><span class="vote_good"> me gusta </span>');
    vote[i].insertAdjacentHTML('beforeend', '<span class="vote_num vote_bad">' + n_neg + '</span>');
 
    $('a', vote[i]).click(function() {
      var that = this,
          links = $('a', that.parentNode);
 
      $.get(that.href, function() {
        var n = that[/minus/.test(that.className) ? 'nextSibling' : 'previousSibling'];
        n.previousSibling.innerHTML = ++n.innerHTML;
        links.fadeOut();
      });
 
 
      links.attr('href', '#').css({
        opacity : 0.4,
        cursor : 'default'
      });
 
      return false;
    });
  }
});
I added the texts to a new separate tag, and added an extra step back in the node list. Let me know if it works okay for you. Wink
avatar
Laura
New Member
Title :
Gender : Unspecified
Posts : 6
Points : 269
Reputation : 4
Language : Spanish, Catalan, English
Browser : Browser : Google Chrome Forum Version : Forum Version : Forumactif Edge
Awards :
View all awards
View user profile

PostLaura on Thu 23 Mar 2017, 02:49

Hi, @Ange Tuteur,

Thank you so much for taking the time to help me. Smile

I've switched to your code and here's what I'm getting now:


avatar
Ange Tuteur
Administrator
Title :
Coffee Addict
Gender : Male
Age : 22
Posts : 4712
Points : 9587
Reputation : 2341
Location : Macungie, PA
Language : EN10, FR5
Browser : Browser : Brave Forum Version : Forum Version : Forumactif Edge
Awards :
View all awards
View user profile https://github.com/SethClydesdale https://twitter.com/sethc1995

PostAnge Tuteur on Thu 23 Mar 2017, 16:17

Whoops I frogot to add previousSibling to the increment. Face palm Try now :
Code:
$(function(){
  for (var vote = $('.vote'), i = 0, j = vote.length, bar, total, percent, n_pos, n_neg; i < j; i++) {
    bar = $('.vote-bar', vote[i])[0];

    if (bar) {
      total = +bar.title.replace(/.*?\((\d+).*/, '$1');
      percent = +bar.title.replace(/.*?(\d+)%.*/, '$1');

      n_pos = Math.round(total * (percent / 100));
      n_neg = total - n_pos;
    } else {
      n_pos = 0;
      n_neg = 0;
    }

    vote[i].insertAdjacentHTML('afterbegin', '<span class="vote_num vote_good">' + n_pos + '</span><span class="vote_good"> me gusta </span>');
    vote[i].insertAdjacentHTML('beforeend', '<span class="vote_num vote_bad">' + n_neg + '</span>');

    $('a', vote[i]).click(function() {
      var that = this,
          links = $('a', that.parentNode);

      $.get(that.href, function() {
        var n = that[/minus/.test(that.className) ? 'nextSibling' : 'previousSibling'].previousSibling;
        n.innerHTML = ++n.innerHTML;
        links.fadeOut();
      });


      links.attr('href', '#').css({
        opacity : 0.4,
        cursor : 'default'
      });

      return false;
    });
  }
});
avatar
Laura
New Member
Title :
Gender : Unspecified
Posts : 6
Points : 269
Reputation : 4
Language : Spanish, Catalan, English
Browser : Browser : Google Chrome Forum Version : Forum Version : Forumactif Edge
Awards :
View all awards
View user profile

PostLaura on Thu 23 Mar 2017, 16:55

That worked! Thank you so much, @Ange Tuteur! Very Happy
avatar
Ange Tuteur
Administrator
Title :
Coffee Addict
Gender : Male
Age : 22
Posts : 4712
Points : 9587
Reputation : 2341
Location : Macungie, PA
Language : EN10, FR5
Browser : Browser : Brave Forum Version : Forum Version : Forumactif Edge
Awards :
View all awards
View user profile https://github.com/SethClydesdale https://twitter.com/sethc1995

PostAnge Tuteur on Thu 23 Mar 2017, 17:19

You're welcome ! ^^

View previous topic View next topic Back to top

Create an account or log in to leave a reply

You need to be a member in order to leave a reply.

Create an account

Join our community by creating a new account. It's easy!


Create a new account

Log in

Already have an account? No problem, log in here.


Log in

 
Permissions in this forum:
You cannot reply to topics in this forum