0

Как сделать, чтобы скрипт работал во время всей работы сайта, а не единожды?

Допустим, у нас есть калькулятор и нам надо выводить значение нажатой пользователем кнопки и, чтобы при каджом следующем нажатии это значение менялось (если пользователь нажимет другую кнопку).

3rd Apr 2018, 5:52 PM
Evil Dog
Evil Dog - avatar
7 Answers
+ 3
https://code.sololearn.com/WxjhPOfOYLtt/?ref=app Вот,тут я сделал,при каждом нажатии увеличивается значение,я не знаю точно,может я тебя неправильно понял)
3rd Apr 2018, 8:23 PM
Nova I Outcast
Nova I Outcast - avatar
+ 1
You'll want to use event listeners for such things. Basically, it's waiting for an event to take place, such as you pressing a button, or any other event. When that event takes place, it'll execute the code associated with it. <button onclick="runThisFunc();">Go!</button> ^In this example, it'll wait for the button to be clicked before it runs the function. You can attach event listeners in a more programmatic type of way with Javascript and/or jQuery. If you haven't learned jQuery, it's a real Godsend, so I'd recommend learning it. It makes dealing with event listeners insanely easier.
3rd Apr 2018, 6:06 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
And if we have <input type='button'>?
3rd Apr 2018, 6:15 PM
Evil Dog
Evil Dog - avatar
+ 1
Exact same concept. "onclick" can be used on many elements. There are other ways to do it directly from your JS/jQuery scripts though, so that's always an option as well. Here is the list of supported/unsupported: Supported HTML tags: All HTML elements, EXCEPT (NOT SUPPORTED TAGS): <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title> https://www.w3schools.com/tags/ev_onclick.asp
3rd Apr 2018, 6:24 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
Can you guess where is a mistake here: var digit = document.getElementById('digit'); digit.onclick = function saveToVar() { var btnValue = document.getElementById('digit').value; document.getElementById('result').innerHTML = btnValue; }; Or should I send full code?
3rd Apr 2018, 7:13 PM
Evil Dog
Evil Dog - avatar
0
А если у нашей кнопки есть значение (value) и мы хотим записать это значение в переменную и вывести?
6th Apr 2018, 9:18 PM
Evil Dog
Evil Dog - avatar
0
https://www.sololearn.com/discuss/2103806/?ref=app Помогите, кто изучает JavaScript
18th Dec 2019, 9:04 PM
Demon EVDK
Demon EVDK - avatar