Kweldulf Kweldulf - 2 years ago 175
jQuery Question

.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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download