How to add jQuery to an HTML page?

jquery

Solution

Including the jQuery Library

That is jQuery code. You'll first need to make sure the jQuery library is loaded. If you don't host the library file yourself, you can hotlink one from the jQuery CDN:

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

You can do this within the `<head>` section, but it's fine as long as it's loaded before your jQuery code.

Further reading:

- Why should I use Google's CDN for jQuery?

- Microsoft CDN for jQuery or Google CDN?

Placing Your Code in the Page

Place your code inside `<script>` tags. It can be inserted anywhere within either `<head>` or `<body>`. If you place it before the `<input>` and `<tr>` tags (as referenced in your code), you have to use `$(document).ready()` to make sure those elements are present before the code is run:

$(document).ready(function() {
    // put your jQuery code here.
});

If you want your page content to be loaded as soon as possible, you might want to place it as close as the `</body>` close tag as possible. But another common practice is to place all JavaScript code in the `<head>` section. This is your choice, based on your coding style and needs.

Suggestion: Instead of embedding JS/jQuery code directly into an HTML page, consider placing the code in a separate .js file. This will allow you to reuse the same code on other pages:

<script src="/path/to/your/code.js"></script>

Further reading:

- Where to place Javascript in a HTML file?

Problem

I was given this chunk of code to place in my html page but I know nothing about javascript so I have no idea where to place it and what kind of tag to place it in. ``` $('input[type=radio]').change(function() { $('input[type=radio]').each(function(index) { $(this).closest('tr').removeClass('selected'); }); $(this).closest('tr').addClass('selected'); }); ```

Original source

Related problems