Kweldulf
Kweldulf

Reputation: 76

.slice() is not a function although called on string

I'm trying to figure out some collision, but the compiler keeps showing the error that .slice() is not a function. Here is the code:

var topPos1 = $('#player').css("top");
var rightPos1 = $('#player').css("right");
var topPos2 = $('#player').css("top");
var rightPos2 = $('#player').css("right");
var pos = topPos1.indexOf('px');
topPos1 = parseInt(topPos1.slice(0,pos));

My jQuery is loaded.

Upvotes: 2

Views: 9514

Answers (3)

Ivan Kovachev
Ivan Kovachev

Reputation: 402

You can just use .replace('px', '') - it will save you one function call

Upvotes: 0

lithium
lithium

Reputation: 229

Maybe your topPos1 return nothing, if the $('#player') has no css value for top, assigning it to a value will result null or NaN.

You should use log the values to see if the result are correct.

Upvotes: 1

Chetan Sanghani
Chetan Sanghani

Reputation: 2111

you can't directly slice a number, you can convert it into string then slice after

like this:

topPos1 = parseInt(topPos1.toString().slice(0,pos));

Upvotes: 0

Related Questions