2

Please I need some help in removing all the white spaces from all the <li> tag ids. The ids are dynamically generated from the title of a podcast rss feeds. How do I convert all the ids (with white spaces) in the following <li> tags:

<li id="The Purpose of Commitment"> ...some content... </li>
<li id="Take Your Seat"> ...some content... </li>
<li id="The Power of the Soil"> ...some content... </li>
<li id="Seasons of Silence"> ...some content... </li>

into ids (without white spaces). Output example below:

<li id="ThePurposeofCommitment"> ...some content... </li>
<li id="TakeYourSeat"> ...some content... </li>
<li id="ThePoweroftheSoil"> ...some content... </li>
<li id="SeasonsofSilence"> ...some content... </li>

I have searched this forum and found similar questions asked here: how to replace all white spaces in a string and here: replace all spaces in a string. I don't know how to accomplish this on the <li> tags. I really appreciate your help. Thanks

xwell
  • 37
  • 1
  • 4
    `The ids are dynamically generated from the title of a podcast rss feeds` <-- You should remove the whitespace during this process. Please post how the ids are dynamically generated. – AndrewL64 Feb 12 '19 at 21:28
  • But if you **HAVE TO** remove them afterwards, you can do that with pure JavaScript using split() and join() as [shown here](https://stackoverflow.com/questions/54658907/remove-white-spaces-from-all-li-tag-ids/#answer-54659021). – AndrewL64 Feb 12 '19 at 21:41
  • Spaces are not your only problem... Arbitrary strings might contain special characters (like single or double quotes) - not really appropriate for IDs, right? :) – Roko C. Buljan Feb 12 '19 at 22:03

4 Answers4

1

Those IDs should be formattted at the point where they are created, not afterwards.

Your problem are not only spaces...

You're using arbitrary strings as ID, and that could include words with special characters, like i.e: Let's Code → You don't want the ID to be id="Let'sCode" ← notice the '

Solution?

Replace everything other than a letter, digit or underscore (regex token: \W) - with underscore.

$("li[id]").prop('id', (i, id) => id.replace(/\W/g, '_'));
<ul>
  <li id="Let's Code"> WARNING! QUOTES! </li>
  <li id="The Purpose of Commitment"> ...some content... </li>
</ul>

<script src="//code.jquery.com/jquery-3.3.1.min.js"></script>

Or by using vanilla JS:

document.querySelectorAll('li[id]').forEach(el => el.id = el.id.replace(/\W/g, '_'));
<ul>
  <li id="Let's Code"> WARNING! QUOTES! </li>
  <li id="The Purpose of Commitment"> ...some content... </li>
</ul>
Community
  • 1
  • 1
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
  • Great answer. I tested the vanilla JS one. It's maybe worth noting that the JS will need to be inside something like window.onload = function(){} for it to work. The OP mightn't know this. – Sarah Feb 12 '19 at 21:45
  • Thank you @Sarah ! Window.load is not necessary (we're talking about DOM Elements here). Placing the ` – Roko C. Buljan Feb 12 '19 at 21:59
  • Oh yes. Sorry I meant document.addEventListener('DOMContentLoaded', function () {}); but nevermind you're right :) – Sarah Feb 12 '19 at 22:22
  • 1
    @Sarah :) yes DOMContentLoaded, or `jQuery($=>{/*doc(ument) you are ready :)*/})` in jQuery. Thx – Roko C. Buljan Feb 12 '19 at 23:32
  • that's exactly it :) – Sarah Feb 13 '19 at 03:25
1

The ids are dynamically generated from the title of a podcast rss feeds <-- You should remove the white-spaces during this process.


But if you HAVE TO remove them afterwards, you can do that with pure JavaScript using a simple split() and join() approach like this:

let lists = document.querySelectorAll("#someUl li");
lists.forEach(list => list.id = list.id.split(" ").join(""));

Check and run the following Code Snippet for a practical example of the above approach:

/* JavaScript */

let lists = document.querySelectorAll("#someUl li");

lists.forEach(list => list.id = list.id.split(" ").join(""));
<!-- HTML -->
<ul id="someUl">
  <li id="The Purpose of Commitment"> ...some content... </li>
  <li id="Take Your Seat"> ...some content... </li>
  <li id="The Power of the Soil"> ...some content... </li>
  <li id="Seasons of Silence"> ...some content... </li>
</ul>
AndrewL64
  • 15,794
  • 8
  • 47
  • 79
0

You can do this with plain Javascript and a regex to replace any number of spaces with nothing:

document.querySelectorAll('li').forEach(li => {
  li.id = li.id.replace(/\s*/g, '');
});

Here is an example which also prints the ids after the transformation:

document.querySelectorAll('li').forEach(li => {
  li.id = li.id.replace(/\s*/g, '');
});

console.log('ids:');
document.querySelectorAll('li').forEach(li => {
  console.log(li.id);
});
<li id="The Purpose of Commitment"> ...some content... </li>
<li id="Take Your Seat"> ...some content... </li>
<li id="The Power of the Soil"> ...some content... </li>
<li id="Seasons of Silence"> ...some content... </li>
jo_va
  • 13,504
  • 3
  • 23
  • 47
-1

If you reference the parent elements inner html and assign it to a js variable you can use a .replace() method to remove the white space and reassign it to the parent.

There may be better ways to do this as I'm quite new myself but I would Google document.getElementbyid().innerhtml.

cfnerd
  • 3,658
  • 12
  • 32
  • 44