<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>JS Change tag inner Text</title>
<style>
#text {
border: 1px solid rgb(117, 117, 117);
padding: 5px;
}
</style>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>
<body>
<table class="w3-table w3-table-all">
<tr>
<th>Модель</th>
<th>Стоимость</th>
</tr>
<tr>
<td class="editable" data-name="model-1-name">Samsung Galaxy S10</td>
<td class="editable" data-name="model-1-price">$1000.00</td>
</tr>
<tr>
<td class="editable" data-name="model-2-name">Apple 7 plus</td>
<td class="editable" data-name="model-2-price">$980.50</td>
</tr>
<tr>
<td class="editable" data-name="model-3-name">Huawei P20 Pro</td>
<td class="editable" data-name="model-3-price">$750.99</td>
</tr>
</table>
<p><button class="w3-button w3-round w3-yellow" onclick="document.querySelector('#new-field-popup').style.display='block'">+ Add field</button></p>
<script src="app.js"></script>
</body>
</html>
function TagContentChanger(selector,onBlurCallback)
{
let elements = document.querySelectorAll(selector);
function process(element,callback)
{
let bg = element.style.background;
element.addEventListener('click', (event) => {
element.setAttribute('contenteditable',true);
element.style.background = "rgb(113, 179, 240)";
});
element.addEventListener('blur', (event) => {
if( element.hasAttribute('contenteditable') ) {
element.removeAttribute('contenteditable',false);
element.style.background = bg;
callback(element);
}
});
}
for(let element of elements) {
process(element,onBlurCallback);
}
}
function fillEditables(selector)
{
let elements = document.querySelectorAll(selector);
for(let element of elements) {
let value = localStorage.getItem(element.dataset.name);
if( !value ) return;
else element.innerText = value.trim();
}
}
fillEditables('.editable');
TagContentChanger('.editable', (element) => {
localStorage.setItem(element.dataset.name,element.innerText);
});
<td class="editable" data-name="model-1-price">$1000.00</td>
<td class="editable" data-name="model-1-price" id = "galaxys10price">$1000.00</td>
<button class = "w3-button w3-round w3-yellow"> change price </button>
<button class = "w3-button w3-round w3-yellow"> return price </button>
let firstPrice = "1000.00" // объявляю переменную начального значения, это не столь важно, для примера сойдет
let galaxyPrice = document.getElementById('galaxys10price') // здесь я получаю один лишь элемент с ID "galaxys10price"
function changeGalaxyPrice(){
galaxyPrice.innerText = "$1200.00" // var.innerText заменяет внутренний HTML текст
}
function returnGalaxyPrice(){
galaxyPrice.innerText = firstPrice // тут то же самое что и в первой функции, но я возвращаю, можете покликать
}
<button class = "w3-button w3-round w3-yellow" onclick = changeGalaxyPrice()> change price </button>
<button class = "w3-button w3-round w3-yellow" onclick = returnGalaxyPrice()> return price </button>