javascript - Adding punctuation to jquery results -
i using jquery find checked items in html form , homecoming sentence including checked items. have sentence construction down, having mental block figuring out how append punctuation sentence, such as: "you have chosen "item1", item2", , "item3". please click each link larn more."
how can add together comma between each item, , add together "and" before lastly one?
i've included little snipped of jquery, if need show more can. thanks!
$("#list").append("you have indicated involvement in "); $.each(linkvalues, function(i, val) { $("#list").append("<a href='" + linkurl[i] + "'>" + val + "</a> "); }); $("#list").append("please click on links larn more these resources."); break; }
adding comma easy:
$.each(linkvalues, function(i, val) { $("#list").append("<a href='" + linkurl[i] + "'>" + val + "</a>, "); // ^^^ });
getting not show after lastly 1 bit harder.
you can observe if it's lastly 1 checking i
, linkvalues.length
:
i == linkvalues.length - 1
then, can utilize ternary operator add together things when it's lastly one:
$.each(linkvalues, function(i, val) { var islast = == linkvalues.length - 1; $("#list").append((islast ? "and " : "") + "<a href='" + linkurl[i] + "'>" + val + "</a>" + (islast ? ", " : " ")); });
javascript jquery
No comments:
Post a Comment