views:

106

answers:

3

Given the following:

<div id="bigchuck">
 <p>blah blah blah.</p>
 <p>yada yada yada.</p>
 <p>Tada. Bing bong the witch is dead. Door bell.</p>
</div>

How can JavaScript/JQUERY find the last sentence "Door bell" and wrap it with a tag, to result in:

<div id="bigchuck">
 <p>blah blah blah.</p>
 <p>yada yada yada.</p>
 <p>Tada. Bing bong the witch is dead. <span>Door bell.</span></p>
</div>

Thank you

+1  A: 

try this:

var plast = $("#bigchuck").find("p:last").text();

var part = plast.split("."), pos = part.length - 2;
if (part.length < 2)
  pos = 0;

part[pos] = "<span>" + part[pos] + ".</span>";
part.pop(); // <-edit for comment

$("#bigchuck").find("p:last").html(part.join("."));
andres descalzo
You're ending up with an extra `.` at the end. I was originally having the same trouble with the final `.` being outside the span.
patrick dw
thanks @patrick
andres descalzo
+2  A: 

Try this: http://jsfiddle.net/NpjUt/

$('#bigchuck p:last').html( function(i,html) {
    var arr = html.split(/(\.)/);
    arr.splice(arr.length - 3, 0, '<span>');
    arr.push('</span>')
    return arr.join('');
});

patrick dw
@patrick - I hope you don't mind me adding the second version. It will wrap just the text and not the space in front of the first word in the last sentence.
Gert G
@Gert - It's a good solution. You should go ahead and add it as an answer. I'd vote for it! :o)
patrick dw
@oatrick - I used your code, so you should have the credit.
Gert G
@Gert - That is very gracious and considerate of you, but the solution is unique enough that it is really yours. I was stuck on the last `.` issue. Yours solves it nicely. I'm going to roll mine back, and will expect to see your answer shortly. :o) You can note that it builds off of mine if you prefer.
patrick dw
@patrick - Thanks. :) But I still think you deserve upvotes for coming up with the `function(i,html)` approach. My first solution was way more bombastic.
Gert G
@Gert - Yeah, passing a function was a nice addition to jQuery 1.4. Good thing about your solution is that it doesn't split on the last `.`, which was giving me a hard time. I didn't even think to add a space in there. Solves that issue perfectly and simplifies the code.
patrick dw
+3  A: 

This solution is based on patrick's solution (he deserves the credit for coming up with the function(i,html) approach), but it just wraps the last sentence and not the space before it:

$('#bigchuck p:last').html( function(i,html) {
  var arr = html.split(". ");
  arr[arr.length-1]="<span>"+arr[arr.length-1]+"</span>";
  return arr.join(". ");
});

Here's the code in action.

Gert G
Gert +1 Very nice. Using `.html( function() {} )` is just a shortcut. It's the guts of the function that matters. I like your solution better. :o)
patrick dw
Thanks patrick. :) Well, I learned a new approach from you. I think that's the beauty with SO. You learn something new every day. :)
Gert G
Gert - Indeed. I've acquired a good part of my jQuery knowledge here.
patrick dw
@patrick - Same here. Earlier today I watched Scott Hanselman's "Social Networking for Developers" (http://ecn.channel9.msdn.com/o9/ch9/3/2/1/1/6/5/HanselminutesOn9SocialNetworkingForDevs1_ch9.wmv). He talked about - among other things - SO, Jon Skeet and how everyone needs at least three places in life. He also talked about how SO is a leap forward compared to the old discussion forums, where you have to dig through all the posts to find the answers. Sorry for going off-topic, but it's a good video and what he says about SO is very true.
Gert G
Gert - Cool. Thanks for the link. I'll be sure to check it out.
patrick dw
Challenge,,, After code runs it's moving the mouse cursor. Any ideas what would be doing that? And if there's a way to prevent it or put it back so the user can keep typing?
WozPoz
Another issue is that it's wrapping incorrectly it's doing: <p>Blah Blah <span> blah blah</p></span> where the span should be before the </p>. Are you seeing this too?
WozPoz
@WozPoz - In which browser are you experiencing this?
Gert G