To include JavaScript in an HTML page use the <script type="text/javascript"> seu script </script>
tag. Everything inside these tags will be interpreted as JavaScript language. In HTML5, <script></script>
is sufficient.
If you include within the <head></head>
tags, the script will run before the page is displayed (ie, if you do not load the script, the page does not open, but when it opens, it already opens with JavaScrit running 100%) , and if enclosed within <body></body>
will be executed during the display.
You can also include JavaScript through an external file, linking it between the <head>
tags:
<script src="_javascript/meu_script.js"></script>
An example of using Javascript to manipulate a DOM element (the script opens a div according to the choice of a button radio
):
var esconderradio = function(var1, var2) {
if (var1) {
document.getElementById(var2).style.display = "";
}
else {
document.getElementById(var2).style.display = "none";
}
};
<label class="radio" for="Csaldsim">
Sim
<input type="radio" id="Csaldsim" name="radio" onclick="esconderradio(true, 'salvenc')"></label>
<label class="radio" for="Csaldnao" onclick="esconderradio(false, 'salvenc')">
Não
<input type="radio" name="radio" id="Csaldnao" onclick="onclick="esconderradio('Csaldim', 'salvenc')"></label>
<br>
<div class=" col-md-3 panel panel-default" id="salvenc" style="display:none;">
<label class="btn" for="Csalvenc">
<input type="number" class="form-control" name="Tsalvenc" id="Csalvenc"></label>
</div>
To study Javascript I suggest codeacademy .