There is a newprice container with data, by pressing the plus button the amount becomes twice as large (например 33 >> 66 >> 99 и т.д.) , and at the press of a button it decreases.

 <div class="newprice m_l"> <?php echo $item->price2; ?> </div>55555 <input type="button" value="plus" onclick="change();"> <input type="button" value="minus" onclick="change('down');"> 

  • 1. And here java, which is specified in the title? 2. Where is any context of joomla, why is its tag set? 3. Is the logic "33 >> 66 >> 99" correct? Perhaps, "33 >> 66 >> 132"? - Alexxosipov

2 answers 2

 function change(_dec) { document.querySelector(".newprice").innerHTML = _dec? +document.querySelector(".newprice").innerHTML / 2 //Если аргумент имеется уменьшаем : +document.querySelector(".newprice").innerHTML * 2 //Если аргумента нет увеличиваем } 
 <div class="newprice m_l">33</div> <input type="button" value="plus" onclick="change();"> <input type="button" value="minus" onclick="change('down');"> 

PS Java and JavaScript - two different languages

    If the newprice initially contains the minimum price, which is also a step of change, then it is better to write this value in the data-attribute of the common container (even though through PHP, even with JavaScript). Despite the greater amount of code, it will be easier - especially if there are several blocks on the page.

    An example of the code that demonstrates this:

      // при готовности DOM уже можем работать с элементами document.addEventListener('DOMContentLoaded', () => { // добавляем всем блокам дата-атрибут с мин.ценой let elems = document.querySelectorAll('.price-block .newprice'); for (let price of elems) price.parentElement.dataset.minPrice = price.textContent; // вешаем обработчики клика elems = document.querySelectorAll('.price-block input[type="button"]'); for (let btn of elems) btn.addEventListener('click', onPriceBtnClick); }); // обработчик клика по кнопкам function onPriceBtnClick(e) { let parent = this.parentElement, priceEl = parent.querySelector('.newprice'), price = +priceEl.textContent; // увеличиваем/уменьшаем на величину мин.цены; присваиваем бОльшую из новой цены, и минимальной price += (this.value === 'plus' ? 1 : -1) * parent.dataset.minPrice; priceEl.textContent = Math.max(price, parent.dataset.minPrice); } 
     .price-block, .price-block > input { display: inline-block; padding: 8px; margin: 4px; font: 16px sans-serif; text-align: center; border: 1px solid #ccc; } .newprice { font-size: 2em; } .price-block > input { min-width: 70px; } .price-block > input[value="minus"] { background: #ebb; } .price-block > input[value="plus"] { background: #add; } 
     <div class="price-block"> <div class="newprice m_l">33</div> <input type="button" value="minus"> <input type="button" value="plus"> </div> <div class="price-block"> <div class="newprice m_l">42</div> <input type="button" value="minus"> <input type="button" value="plus"> </div>