Paul Paul - 4 years ago 125
Javascript Question

Making text singular or plural based on variable length

I am trying to make text singular or plural based on the value of

. If
's value is 1 then I just want 'Template' to say that, but if
is plural, I want it to say 'Templates Selected'.

What am I doing wrong with my code?

$('#templateCount').html(templateCount + " Templates Selected" + templateCount.length == 1 ? "" : "s");

Answer Source

Are you sure you thought this through? You have the string "Templates selected" there and you are conditionally appending s to the end of that (which would make it "Templates Selecteds").

Do this:

$('#templateCount').html(templateCount + 
    " Template" + 
    (templateCount === 1 ? "" : "s") +
    " Selected");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download