Reputation: 2466
So my img src generates like this img/0001/name0001/img0001.jpg
and im trying to grab /name0001/
only any idea ? thanks!
my code:
var parent = $('.img');
$(parent, document).click(function() {
var dirz = this.src;
var dirb = dirz.split('/')[2];
alert(dirb);
});
it alerts with blank?
Upvotes: 0
Views: 135
Reputation: 339786
If the file names are all in that format,
var dir = filename.split('/')[2];
Note that if you use the .src
property of the image then it will include the full URL, including the scheme and hostname, which is not the same as the input shown in the question.
To parse the path as supplied in the HTML, you need to read the attribute using .attr('src')
Upvotes: 2
Reputation: 2653
You can use the below code
var src = "img/0001/name0001/img0001.jpg",
segment = src.split('/')[2]; // "name0001"
Thanks
Upvotes: 0
Reputation: 6346
use the split method:
str = "img/0001/name0001/img0001.jpg";
var n=str.split("/");
then just grab n[2]
Upvotes: 0
Reputation: 16510
How about splitting on /
and grabbing the corresponding segment?
var src = "img/0001/name0001/img0001.jpg",
segment = src.split('/')[2]; // "name0001"
Upvotes: 1
Reputation: 55740
var src = 'img/0001/name0001/img0001.jpg';
var items = src.split('/')[2] ;
Upvotes: 1