D
D
Dmitry Larin2018-01-31 15:29:20
JavaScript
Dmitry Larin, 2018-01-31 15:29:20

How to properly integrate JS and HTML?

As it is more correct, it is "beautiful" to write JS code. Using jQuery as an example.
So:
<button id="click">Кнопка</button>

$('document').ready(function(){
$('#click').click(function(){
console.log(123);
})
})

+
HTML is not overloaded with code
-
It is difficult to find dependencies in a large project
Or like this:
<button onclick="click()">Кнопка</button>
function click(){
console.log(123);
}

+
it is immediately clear who is launching what
-
overloaded HTML

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander Litvinenko, 2018-01-31
@fanrok

in fact, depending on how you write, here, apparently, you have the most primitive layout option, for it inline events (onclick="click()") are used only as a last resort, since they are not obvious in the code.
and yet you do not apply an alert, this is a blocking operation.

O
OKyJIucT, 2018-01-31
@OKyJIucT

I won't say which is correct, but I usually use the first method.

E
Exploding, 2018-01-31
@Exploding

But in the second option - there will be no "doubling / x4 / x8 / ..." listeners and "why the code does not work when I use Ajax".

D
dom1n1k, 2018-01-31
@dom1n1k

Definitely the first option.
The second can only be used for quick tests on the knee, when you need to quickly check something and throw it away.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question