0

This is the markup

<html>
<head>
    <title>Youtube App</title>
    <script src="script.js"></script>
    <link href="style.css" rel="stylesheet">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
    <div id="container">
        <header>
            <h1>Search<span>Up<span></h1>
            <p>Search all Youtube videos</p>
        </header>
            <section>
                <form id="searchForm" name="searchForm" onsubmit="return search()">
                    <div class="fieldcontainer">
                        <input type="search" id="query" class="search-field" placeholder="Search Youtube...">
                        <input type="submit" name="search-btn" id="search-btn" value="">
                    </div>
                </form>
                <ul id="results"></ul>
                <div id="buttons"></div>        
            </section>  
            <footer>
                <p>Copyright &copy; 2017, All Rights Reserved</p>
            </footer>
</body>
</html>

and this is the script

$(function(){   
$('searchForm').submit(function(e){
    e.preventDefault();
});
    });
function search(){
    //clear results
    $('#results').html('');
    $('#buttons').html('');

    //Get Form Input
    q = $('#query').val();

    //Run GET Requests on API
    $.get(
        "https://www.googleapis.com/youtube/v3/search",{
                part: 'snippet, id',
                q: q,
                type: 'video',
                key: '123'},
            function(data){
                var nextPageToken = data.nextPageToken;
                var prevPageToken = data.prevPageToken;

            console.log(data);
            }
        )
    }

well the problem is even though i have given the links

<script src="script.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

it still says $ is not defined and MOST importantly "console.log(data);" this is how i load the data from search of the youtube api which should console log but also not working Please help and thank you...

Neji Soltani
  • 1,522
  • 4
  • 22
  • 41

3 Answers3

1

JS files are loaded synchronously in the browser unless you specify otherwise. Meaning, if you need to use jQuery in your custom JS file, you need to import it before your JS file loads. Your code will get jQuery, if you change the import order of your files as follows:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="script.js"></script>
Ivan
  • 34,531
  • 8
  • 55
  • 100
Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
1

Here's the working code in Plunker , you can find the index and the script files.
PS: you need to change the api key

index.html

<html>
<head>
    <title>Youtube App</title>


    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="script.js"></script>
</head>
<body>
    <div id="container">
        <header>
            <h1>Search<span>Up<span></h1>
            <p>Search all Youtube videos</p>
        </header>
            <section>
                <form id="searchForm" name="searchForm" >
                    <div class="fieldcontainer">
                        <input type="search" id="query" class="search-field" placeholder="Search Youtube...">
                        <input type="button" name="search-btn" id="search-btn" value="submit" onclick="search()">
                    </div>
                </form>
                <ul id="results"></ul>
                <div id="buttons"></div>        
            </section>  
            <footer>
                <p>Copyright &copy; 2017, All Rights Reserved</p>
            </footer>
</body>
</html>

Script.js

function search(){

    //clear results
    $('#results').html('');
    $('#buttons').html('');

    //Get Form Input
    q = $('#query').val();

    //Run GET Requests on API
    $.get(
        "https://www.googleapis.com/youtube/v3/search",{
                part: 'snippet, id',
                q: q,
                type: 'video',
                key: '!!your api key!!!'},
        ).done(function( data ) {
                var nextPageToken = data.nextPageToken;
                var prevPageToken = data.prevPageToken;



                var jsonPretty = JSON.stringify(data, null, '\t');

                $("#results").html(jsonPretty);

            console.log(data);   }); }
Neji Soltani
  • 1,522
  • 4
  • 22
  • 41
0
<link href="style.css" rel="stylesheet">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="script.js"></script>

use script.js after jquery

Neeraj Pathak
  • 759
  • 4
  • 13