I’m having an issue with some ellipsis being automatically added the end of a link with something I am pulling into my site. I really don’t know javascript very well, and am trying to add something in to remove that!
Here’s the code (with a fake link):
<div class="item-title-container">
<a title="Brewery Packaging Technology Course" href="https://www.link">Brewery Packaging Technology Course...</a>
</div>
Here’s what I’ve tried (3 different options):
let srch = '...';
document.querySelectorAll('.item-title-container').forEach(el => {
if (el.innerText.indexOf(srch) > -1) el.innerText = el.innerText.split(srch).slice(0, -1).join(srch);
})
let element = document.querySelector('.item-title-container');
element.innerHTML.replace('...','');
var str = document.getElementsByClassName('item-title-container');
str = str.slice(0, -3);
I am obviously not very good at this but looking for a solution!
2
Answers
It’s probably an ellipsis character, not three
.
characters. You can use a regexp that replaces either.Fixing the 2nd Approach
.innerHTML
allows you to access and set the content of a div or some other HTML tags.However, you cannot edit the value of
.innerHTML
, this may sound confusing, here are some examples.You cannot access and set the content with the same
.innerHTML
, and that’s what you tried by typingelement.innerHTML.replace('...','');
.To fix that try,
Fixing the 3rd Approach
When
.innerHTML
is used to access the content, it does not return a reference to the content. That’s the reason why the following code is not working:In the code above, editing
str
will only editstr
. In other words,str
is not reference to the element’sinnerHTML
. In order to make the code work, you have set theinnerHTML
tostr
.Try the following:
To make the code above easier to read try the following: