Users can input their name on my HTML form, which will then be displayed in a DIV as part of a book title.
The book title includes apostrophes (e.g. Amy's Holiday Album).
However, if the user's name ends with an S, I do not want the apostrophe s to be included (e.g. Chris' Holiday Album instead of Chris's Holiday Album).
This should only happen if the form has a class of apostrophe
. Otherwise, the name should be copied without any apostrophes or 's' added.
I attempted to use slice() along with an if statement to achieve this, but it did not work as expected.
Below is the HTML code:
<div><b class="title"></b> Holiday Album</div>
And here is the Jquery code (1.8.3):
$(document).ready(function() {
$('.name').keyup(function() {
var finalname = text($(this).val());
var lastChar = finalname.slice(-1);
var finaltitle;
if ($(".apostrophe")[0]) {
if (lastChar == 's') {
finaltitle = finalname + "'";
}
else {
finaltitle = finalname + "'s";
}
$('.title').text(finaltitle);
}
else {
$('.title').text(finalname);
}
});
});