0

I currently have a problem with JavaScript and HTML. I want to edit the text of the button without removing the image which is in the same element. The line I want to edit:

<a href="warenkorb.html" class="artikel"><img src="warenkorb.png" height="25vh" id="warenkorb">Warenkorb</a>

What I have tried so far:

function warenAnzahl() {
    var artikel = localStorage.getItem("artikel")
    var x = document.getElementsByClassName("artikel")
    var i;
    if (artikel > 0) {
        for (i = 0; i < x.length; i++) {
            x[i].textContent = "Warenkorb (" + artikel + ")";
        }
    }
}

setInterval(warenAnzahl, 100)

The text gets changed how it should, but the image gets removed too with that method. Neither the text, nor the image, should move because it's already in my navbar.

chazsolo
  • 7,873
  • 1
  • 20
  • 44
Tiiill
  • 47
  • 1
  • 3

3 Answers3

2

You can wrap the text inside a <span> and add a class to this span, then select this class instead of "artikel" class.

Vega
  • 27,856
  • 27
  • 95
  • 103
0

instead of

x[i].textContent = "Warenkorb (" + artikel + ")";

try

x[i].innerHTML += " (" + artikel + ")";

this will add in the (artikel) to the end of the link, which looks like what you're trying to do.

XTOTHEL
  • 5,098
  • 1
  • 9
  • 17
0
var appendImg = $('.artikel').children();
$('.artikel').text('Altered Text').append(appendImg);very simple ,

very simple, As soon as the text is changed it deletes all the children as we allready know ,

So save the children and then append them at the end (after renaming)