EasyBB
EasyBB

Reputation: 6554

Counting a hrefs then subtracting

$(function() {
 var getName = $('#fa_welcome').text();
 var myName = getName.replace('Welcome ',"");
 var a = $('#recent_topics').find('a[href*="/u"]').filter(':contains("'+myName+'")').length;
 var b = $('#recent_topics').find('a[href*="/u"]').length;
 var c = a-b;
  if(c <= 1) {
  $('.topics_name').append('<div title="'+c+' New Post" id="newTops">'+c+'</div>');
  }
  });

Just for randomness I will give you the lengths of var a and b

a= 15 b= 25

I want to subtract these two as in var c

15-25

Though I get -25 parsed?

Any suggestions

Upvotes: 0

Views: 58

Answers (3)

UdayKiran Pulipati
UdayKiran Pulipati

Reputation: 6667

use parseFloat for getting result

$(function() {
 var getName = $('#fa_welcome').text();
 var myName = getName.replace('Welcome ',"");
 var a = parseFloat($('#recent_topics').find('a[href*="/u"]').filter(':contains("'+myName+'")').length);
 var b = parseFloat($('#recent_topics').find('a[href*="/u"]').length);
 var c = a-b;
  if(c <= 1) {
  $('.topics_name').append('<div title="'+c+' New Post" id="newTops">'+c+'</div>');
  }
  });

Upvotes: 0

Rohan Kumar
Rohan Kumar

Reputation: 40639

If you get these values then, use parseInt to convert it in integer,

var c = parseInt(a)-parseInt(b);

Upvotes: 0

GautamD31
GautamD31

Reputation: 28763

Try with parseInt like

var a = parseInt($('#recent_topics').find('a[href*="/u"]').filter(':contains("'+myName+'")').length);
 var b = parseInt($('#recent_topics').find('a[href*="/u"]').length);

Upvotes: 2

Related Questions