2

I am working on an HTML list full of names, and each name is written in this format:

"firstname-lastname/" (example: john-smith/)

So I was curious to know if I could use JavaScript to change the format of the text into:

"Firstname Lastname" (example: John Smith)

As I am relatively new to JavaScript, and I haven't done much work with it the language yet, and I wasn't able to do this.

Here is a snippet of the HTML list:

<ul>
<li><a href="john-smith/"> john-smith/</a></li>
<li><a href="joe-smith/"> joe-smith/</a></li>
<li><a href="gina-smith/"> gina-smith/</a></li>
<li><a href="tom-smith/"> tom-smith/</a></li>
<li><a href="peter-smith/"> peter-smith/</a></li>
</ul>

Also, if I wasn't clear enough, I do not want to change the href, just the actual text that is displayed.

Mi-Creativity
9,66410 gold badges40 silver badges49 bronze badges
asked Aug 7, 2016 at 6:14
4
  • Are you generating this html, or are you simply wanting to modify it afterwards? Commented Aug 7, 2016 at 6:15
  • @FrankerZ I believe that this HTML was generated, I was sent this HTML list and I wanted to give it some adjustments. Commented Aug 7, 2016 at 6:18
  • I've posted below how you can modify the string. If you'd like to post a larger HTML chunk, I can show you how to modify the contents of the anchor tags with what you want. Commented Aug 7, 2016 at 6:20
  • Added a larger html chunk! @FrankerZ Commented Aug 7, 2016 at 6:27

2 Answers 2

5

You can do this with Regex:

var REGEX_FIND = /(.*?)-(.*?)\/$/;
//From: http://stackoverflow.com/questions/1026069/capitalize-the-first-letter-of-string-in-javascript
function capitalizeFirstLetter(string) {
 return string.charAt(0).toUpperCase() + string.slice(1);
}
$('ul li a').each(function() {
 var text = $(this).text().trim();
 
 var m;
 
 if ((m = REGEX_FIND.exec(text)) !== null) {
 $(this).text(capitalizeFirstLetter(m[1]) + ' ' + capitalizeFirstLetter(m[2]));
 }
});
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li><a href="john-smith/"> john-smith/</a></li>
<li><a href="joe-smith/"> joe-smith/</a></li>
<li><a href="gina-smith/"> gina-smith/</a></li>
<li><a href="tom-smith/"> tom-smith/</a></li>
<li><a href="peter-smith/"> peter-smith/</a></li>
</ul>

Mi-Creativity
9,66410 gold badges40 silver badges49 bronze badges
answered Aug 7, 2016 at 6:19
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the code, but how could I loop through each of my list items and change the HTML text from there? Sorry, I'm new to JavaScript!
Check my comment above on your question @Dan
Add the script to the bottom of your html (Right before the </body> tag), or wrap your functions in a $(document).ready(function() { /* Your code here */ }); tag. Because the HTML isn't actually initialized at the time the code is run, $('ul li a') doesn't find any elements, so the code is not run.
For this kind of mission you can feel free to use var tom_link = document.querySelectorAll("a[href='tom-smith']");, jQuery is a bit overkill.
@FrankerZ I now see why that didn't work; I learned a lot! Thank you so much for the help!
3

Alternatively you can do it like this

jsFiddle

// target the anchor tags inside the list items
var namesList = document.querySelectorAll('#namesList li a');
// loop through
for (var i = 0; i < namesList.length; ++i) {
 // text value, use trim to get rid of spaces on right and left sides of the string
 var text = namesList[i].textContent.trim(), 
 
 // number of chars in text - 1, thus we automatically eliminate the last 
 // char "/" in each string
 textL = text.length - 1, 
 firstName, lastName, theIndex;
 // we determine the number where the symbol - lies 
 theIndex = text.indexOf('-');
 
 // for firstName, turn the first char to upper case, and append 
 // characters from the second char to the index of "-" minus 1
 firstName = text.charAt(0).toUpperCase() + text.substring(1, theIndex);
 
 // for lastName, turn the first char AFTER the index of "-" to upper case, and append 
 // characters from index + 1 to the value of text length
 lastName = text.charAt(theIndex + 1).toUpperCase() + text.substring(theIndex + 2, textL);
 console.log(firstName + ' ' + lastName);
 
 // join firstName and lastName with space in between 
 namesList[i].textContent = firstName + ' ' + lastName;
}
<ul id="namesList">
 <li><a href="john-smith/"> john-smith/</a></li>
 <li><a href="joe-smith/"> joe-smith/</a></li>
 <li><a href="gina-smith/"> gina-smith/</a></li>
 <li><a href="tom-smith/"> tom-smith/</a></li>
 <li><a href="peter-smith/"> peter-smith/</a></li>
</ul>

answered Aug 7, 2016 at 7:01

2 Comments

Nice alternative way
yeah but it doesn't work for IE8 and below, there're polyfills though

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.