Difficulties in connecting Javascript with HTML [closed]

2

Some months ago I took a course to enter the job market as "Front End", I learned what was needed, but now to put into practice Javascript is complicated, because in theory I know a lot with Javascript, but at the time of connecting these things with the DOM is difficult.

Do you have any tips or ways to study?

Thank you for your attention.

    
asked by anonymous 09.06.2015 / 21:09

1 answer

2

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 .

    
09.06.2015 / 21:46