I am getting CSS ‘left’ property value. now it is set on -50. so i want to get only 50 can this be done with split function or there is another way to do that. Also why split function is not working in my function
<head>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.box').click(function (){
var kar=parseInt($(this).css('left'))
var jj= kar.split('')
alert(jj[0])
})
});
</script>
<style>
.container {
margin:auto;
width:500px;
position:relative
}
.box { background:#FF0000; width:200px; height:200px; position:absolute;left:-50px;}
</style>
</head>
<body>
<div class="container">
<div class="box"></div>
</div>
</body>
I think what you are looking for is the Math.abs function:
The split function works on strings and returns an array with all parts of the string separated by the given delimiter. You are giving an empty string as delimiter, which splits your string after each character like this:
If you are getting a string back like
"-50px", then you can do it like this:Also: there is no jQuery involved in this (besides the
.css()function),splitandabsare functions of JavaScript’s predefined core objectsStringandMath.