Last active
April 23, 2018 14:18
-
-
Save DevShahidul/3d889d62208a00a6714472df921d7a60 to your computer and use it in GitHub Desktop.
See example >>> https://stackoverflow.com/questions/5458605/first-word-selector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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