test
test

Reputation: 2466

jQuery grab specific img src folder name

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

Answers (5)

Alnitak
Alnitak

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

Sridhar Narasimhan
Sridhar Narasimhan

Reputation: 2653

You can use the below code

var src = "img/0001/name0001/img0001.jpg",
segment = src.split('/')[2]; // "name0001"

Thanks

Upvotes: 0

Matanya
Matanya

Reputation: 6346

use the split method:

str = "img/0001/name0001/img0001.jpg";    
var n=str.split("/");

then just grab n[2]

Upvotes: 0

rjz
rjz

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

Sushanth --
Sushanth --

Reputation: 55740

var src = 'img/0001/name0001/img0001.jpg';

var items = src.split('/')[2]  ;

Upvotes: 1

Related Questions