Kamal
Kamal

Reputation: 2180

split() using jQuery

Hi Friends trying to split multiple charterers from a string using split() function but its not working please check my code below or u can see fiddle here

I just want to get image name without its extention like .png , .jpg etc.

HTML

<ul data-role="list-divider" class="footerMenu">
        <li><a href="#"><img src="images/friendsIcon.png" width="114" height="77" alt=" " /><br />Friends</a></li>
        <li><a href="#"><img src="images/cardsIcon.png" width="114" height="77" alt=" " /><br />Cards</a></li>
        <li><a href="#"><img src="images/egreetingIcon.png" width="114" height="77" alt=" " /><br />Egreeting</a></li>
        <li><a href="#"><img src="images/post.png" width="114" height="77" alt=" " /><br />Post</a></li>

        <li><a href="#"><img src="images/customIcon.png" width="114" height="77" alt=" " /><br />Custom</a></li>
    </ul>

SCRIPT

   $('.footerMenu li a').on('click',function(){
       var wh = 'wh';
       var whe = $(this).children('img').attr('src').split('images/' , '.png');
       //var spl = whe.split('.png')
       alert(whe);
       if($(this).hasClass('.ui-btn-active'))
       {
           $(this).siblings('img')
           }
       })

Please help me friends... Thanks in advance

Upvotes: 0

Views: 714

Answers (2)

Liftoff
Liftoff

Reputation: 25392

split() returns an array of all of the strings separated by the provided string. You want to use replace()

var pattern = ".png";
var file = someString.replace(new RegExp(pattern, "g"));

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

Use a regex

$('.footerMenu li a').on('click', function () {
    var src = $(this).children('img').attr('src');
    var whe = src.match(/([^\/]+).png/)[1];
    //var spl = whe.split('.png')
    alert(whe);
    if ($(this).hasClass('.ui-btn-active')) {
        $(this).siblings('img')
    }
})

Demo: Fiddle

Upvotes: 1

Related Questions