I can get background image by doing
$('#item').css('background-image').
but the result is url('www.example.com/something/123.png')
How can get get only the 123 value? I can't use string position because the value of the img name doesn't' necessary is 3 characters.
Use the . substring() method to get the access the string after last slash.
To get the last two characters of a string in JavaScript, call the slice() method on the string, passing -2 as an argument. For example, str. slice(-2) returns a new string containing the last two characters of str .
You can just use substr() for this.
You can use lastIndexOf
and substring
:
var uri= $('#item').css('background-image');
var lastslashindex = uri.lastIndexOf('/');
var result= uri.substring(lastslashindex + 1).replace(".png","");
or using .split()
and .pop()
var uri= $('#item').css('background-image');
var result=uri.split("/").pop().replace(".png","");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With