8

I'm trying to do something that would be similar to turning a url slug-like variable into text that could be used for a title.

So, I have a variable for example that is like this:

var thisID = 'athlete-profile';

function myFunc(thisID) {
    // i need to use thisID as the id and href in a loop that generates a string of <li><a>'s\

    function makeTitle(thisID) {
        // convert thisID to text so for this example it would return 'Athlete Profile'
        return 'Athlete Profile';
    }

    for () {
        var str = '<li id="'+thisID+'"><a href="#'+thisId+'">'+makeTitle(thisID)+'</a>';
    }
    // make sense?
}

I'd like to not use a regex to do this if possible somehow, but I don't think there's a way to do it without one. So any one who knows how to do this type of thing let me know, it would be a great help.

Thanks

jaredwilli
  • 11,762
  • 6
  • 42
  • 41
  • What is that `for` loop doing? Element ids need to be unique, so you shouldn't be creating elements in a loop and giving them all the same id. – nnnnnn Jan 24 '12 at 01:03
  • no, thisID is always a different id, and actually, i meant to make it
  • – jaredwilli Jan 24 '12 at 01:06
  • If you just want to change hyphens to spaces it is super simple: `thisID.replace(/-/g, " ")` (as shown in the first part of my answer). But doing the capitalisation is also easy as shown in any of the answers below. – nnnnnn Jan 24 '12 at 01:42