curly_brackets
curly_brackets

Reputation: 5598

Remove part of filename with jQuery

I want to strip a filename like this: IMG_6903.JPG&width=504

to:

IMG_6903.JPG

...

My script looks like this:

  $(function(){
    $('#exposure img').each(function(){
        var $imgSrc = this.src.split('&');
        $(this).wrap('<a rel="lightBox" />')
               .parent().attr("href", ""+$imgSrc);
      });
  });    

But it doesn't work... How do I do this?

Upvotes: 1

Views: 543

Answers (4)

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25475

You split the string but try using the array instead of the first element. Try this

 $(function(){
     $('#exposure img').each(function(){
         var $imgSrc = this.src.split('&');
         $(this).wrap('<a rel="lightBox" />')
             .parent().attr("href", "" + $imgSrc[0]);
     });
 });

Hope this helps.

Upvotes: 0

Dutchie432
Dutchie432

Reputation: 29170

var imgSrc = this.src.substring(0,this.src.indexOf('&'));

Upvotes: 2

Gowri
Gowri

Reputation: 16845

use split

var str = 'IMG_6903.JPG&width=504';
var array = str.split('&');
array[0];

array[0] will be IMG_6903.JPG

Upvotes: 0

Anthony Graglia
Anthony Graglia

Reputation: 5435

var andIndex = tmpString.indexOf('&');
tmpString = tmpString.substr(0, andIndex);

Upvotes: 0

Related Questions