dikei
dikei

Reputation: 439

I am trying to wrap a return value from a function with an html element

Hi I am using the suffix function to add the th, st and rd to numbers, but I am trying to wrap the actual suffix with a span, somehow it doesn't work, not sure why.

Thanks for the help, it worked switch it to html, is there any chance to remove the counter itself, as I am getting all the numbers from the DB, just need to add the suffix to the number, not both. I removed the numbers from the divs and still get the numbers.

here is the code

<div></div>
<div></div>
<div></div>
<div></div>

$("div").html(function (i, t) {
        i++;
        var str = i.toString().slice(-1),
            suffix = '';
        switch (str) {
            case '1':
                suffix = i.toString().slice(-2) === '11' ? 'th': 'st';
                break;
            case '2':
                suffix = i.toString().slice(-2) === '12' ? 'th' : 'nd';
                break;
            case '3':
                suffix = i.toString().slice(-2) === '13' ? 'th' : 'rd';
                break;
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            case '0':
                suffix = 'th';
                break;
        }
         return i + '<span>' + suffix + '</span>';
    });

Upvotes: 0

Views: 72

Answers (1)

Dhunt
Dhunt

Reputation: 1594

does it work if you change .text() to .html()?

<div>1</div>
<div>2</div>    
<div>3</div>
<div>4</div>

$("div").html(function (i, t) {
    i++;
    var str = i.toString().slice(-1),
        suffix = '';
    switch (str) {
        case '1':
            suffix = i.toString().slice(-2) === '11' ? 'th': 'st';
            break;
        case '2':
            suffix = i.toString().slice(-2) === '12' ? 'th' : 'nd';
            break;
        case '3':
            suffix = i.toString().slice(-2) === '13' ? 'th' : 'rd';
            break;
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
        case '0':
            suffix = 'th';
            break;
    }
     return i + '<span>' + suffix + '</span>';
});

example: http://jsfiddle.net/2d02k5ou/

Upvotes: 2

Related Questions