Skip to content

Instantly share code, notes, and snippets.

@DevShahidul
Last active April 23, 2018 14:18
Show Gist options
  • Select an option

  • Save DevShahidul/3d889d62208a00a6714472df921d7a60 to your computer and use it in GitHub Desktop.

Select an option

Save DevShahidul/3d889d62208a00a6714472df921d7a60 to your computer and use it in GitHub Desktop.
function wrapFirstWord () { // This function for select first word.
// Select only the first text node
var node = $("div").contents().filter(function () {
return this.nodeType == 3;
}).first(),
// Get the text...
text = node.text(),
// ... and the first word
first = text.slice(0, text.indexOf(" "));
if (!node.length)
return;
// Remove the first word from the text
node[0].nodeValue = text.slice(first.length);
// Add it back in with HTML around it
node.before('<span>' + first + '</span><br/>');
};
// Another way to select First word and last word =========================
$("#lastWord").html(function(){ // Class or id can be use
var text = $("#lastWord").text().trim().split(" "); // All text convert to array.
var last = text.pop(); // Select last wrod.
var first = text.shift(); // Select first word.
var firstW = first.innerHTML = '<span class="firstWord">' + first + '</span>'; // Make wrapper of first wrod
var lastW = last.innerHTML = '<span class="lastWord">' + last + '</span>'; // Make wrapper of last wrod.
return firstW + " " + text.join(" ") + " " + lastW ;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment