$('button').on('click', function(){
var str = $.map($('.bimg'), (e) => $(e).attr('src')).join(',');
console.log(str);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img class='bimg' src='gall/lorem.jpg' alt='img'>
<img class='bimg' src='gall/ipsum.jpg' alt='img'>
<img class='bimg' src='gall/dolor.jpg' alt='img'>
<button>CLICK</button>
Result:
gall/lorem.jpg,gall/ipsum.jpg,gall/dolor.jpg
What I need:
lorem,ipsum,dolor
CodePudding user response:
You can use regex
to get the image name from src
.
$('button').on('click', function() {
var str = $.map($('.bimg'), (e) => $(e).attr('src').match(/\/(.*)\.jpg/)[1]).join(',')
console.log(str);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img class='bimg' src='gall/lorem.jpg' alt='img'>
<img class='bimg' src='gall/ipsum.jpg' alt='img'>
<img class='bimg' src='gall/dolor.jpg' alt='img'>
<button>CLICK</button>
CodePudding user response:
Add the following to your map
.split('/').pop().split('.').shift()
So we split on each /
and use pop()
to get the last one found
Then we'll split on each .
and use shift()
to get the first part
$('button').on('click', function(){
var str = $.map($('.bimg'), (e) => $(e).attr('src').split('/').pop().split('.').shift()).join(',');
console.log(str);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img class='bimg' src='gall/lorem.jpg' alt='img'>
<img class='bimg' src='gall/ipsum.jpg' alt='img'>
<img class='bimg' src='gall/dolor.jpg' alt='img'>
<button>CLICK</button>
lorem.jpg,ipsum.jpg,dolor.jpg