A
A
Anton2018-03-20 07:24:45
JavaScript
Anton, 2018-03-20 07:24:45

How to change link text after clicking link in JS?

For example:
<a href="#">Добавить товар</a>
Replace after clicking on:
<a href="#">Добавлено</a>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dlyatorrenta1, 2018-03-20
@dlyatorrenta1

Without jquery, somehow, instead of getElementById, you can use other type selectors: getElementsByTagName, getElementsByClassName, querySelectorAll ( read more here ), but keep in mind that they return an array of elements, unlike getElementById and querySelector. They should be used in order not to give all elements a unique id if there are many of them.

<script>
document.getElementById('demo').addEventListener('click', function (event) {
   event.preventDefault(); //отключает переход по ссылке
   this.innerHTML = 'Добавлено';
})
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question