Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8152447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:43:06+00:00 2026-06-06T15:43:06+00:00

EDIT 2: I have just realised that my HTML is only displaying the artist

  • 0

EDIT 2:

I have just realised that my HTML is only displaying the artist & song, not the album which is why when I had

alert(album);

in there I was getting unexpected results. The jQuery trim is now working as expected.
Blush
Thank you for your answers which I will keep for other scripts.

I’m using JQuery to grab the last 10 songs played from a lastfm user and display them.

I would like to truncate the album names down to x characters and replace with …

The string is:

album = item.album['#text'];

and it is displayed here:

$current.find('[class=lfm_album]').append(album);

I have tried using:

var shortText = jQuery.trim(album).substring(0, 10)
    .split(" ").slice(0, -1).join(" ") + "...";

but it just displays the full album string and no amount of fiddling or javascript seems to work.

EDIT:

I have just tried

album = item.album['#text'];
alert(album);

to see what it thinks is there and I get some very results that don’t match any of the other strings produced by the script so really am stuck now!

The full script is as follows:

/*---------------
 * jQuery Last.Fm Plugin by Engage Interactive
 * Examples and documentation at: http://labs.engageinteractive.co.uk/lastfm/
 * Copyright (c) 2009 Engage Interactive
 * Version: 1.0 (10-JUN-2009)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Requires: jQuery v1.3 or later
---------------*/

(function($){
    $.fn.lastFM = function(options) {

        var defaults = {
            number: 5,
            username: 'xxxxxxx',
            apikey: 'xxxxxxxx',
            artSize: 'medium',
            noart: 'images/noartwork.gif',
            onComplete: function(){}
        },
        settings = $.extend({}, defaults, options);

        var lastUrl = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user='+settings.username+'&api_key='+settings.apikey+'&limit='+settings.number+'&format=json&callback=?';
        var $this = $(this);

        var container = $this.html();

        $this.children(':first').remove();

        if(settings.artSize == 'small'){imgSize = 0}
        if(settings.artSize == 'medium'){imgSize = 1}
        if(settings.artSize == 'large'){imgSize = 2}

        this.each(function() {

            $.getJSON(lastUrl, function(data){ 
                $.each(data.recenttracks.track, function(i, item){

                    if(item.image[1]['#text'] == ''){
                        art = settings.noart;
                    }else{
                        art = stripslashes(item.image[imgSize]['#text']);
                    }

                    url = stripslashes(item.url);
                    song = item.name;
                    artist = item.artist['#text'];
                    album = item.album['#text'];
// this is the part where I try to truncate "album"
var shortText = $.trim(album).substring(0, 10)
    .split(" ").slice(0, -1).join(" ") + "...";




                    $this.append(container);

                    var $current = $this.children(':eq('+i+')');

                    $current.find('[class=lfm_song]').append(song);
                    $current.find('[class=lfm_artist]').append(artist);
                    //$current.find('[class=lfm_album]').append(album);
                    $current.find('[class=lfm_album]').append(shortText);
                    $current.find('[class=lfm_art]').append("<img src='"+art+"' alt='Artwork for "+album+"'/>");
                    $current.find('a').attr('href', url).attr('title', 'Listen to '+song+' on Last.FM').attr('target', '_blank');

                    //callback
                    if(i==(settings.number-1)){
                        settings.onComplete.call(this);
                    }

                });
            });
        });
    };

    //Clean up the URL's
    function stripslashes( str ) {   
        return (str+'').replace(/\0/g, '0').replace(/\\([\\'"])/g, '$1');
    }
})(jQuery);

thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:43:08+00:00Added an answer on June 6, 2026 at 3:43 pm

    If you’re trying to truncate the album/title by spaces after 10th character you can use the indexOf method

    var album = "   This is the're long title of the album    ";
    
    album = $.trim(album); //make sure to trim the album first
    if (album.length > 10) {
      //the indexOf will return the position of the first space starting from the 10th
       alert(album.substring(0, album.indexOf(' ',10)) + "...");
    }
    

    Source MDN

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just successfully installed CakePHP and I see that I can edit the
(Edit: I've since realised that perhaps it's not possible to pass arrays as parameters
I have just realised that JSlider cannot deal with floating point numbers. Can anybody
I won't want to have edit any working sets. I just want a way
I have to edit my question just to make it clear. What I need
Edit: just to be clearer on what I am looking to do. I have
EDIT: I have realized the source of my problem. I only have count information
I have a quick question regarding memory management that I am not quite sure
I am dealing with some HTML text that I basically have read access to.
I have a client that has just moved a very larger, well established wordpress

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.