Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add jQuery to an HTML page? [duplicate]

Tags:

jquery

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');
});
like image 902
user1888584 Avatar asked Dec 11 '12 22:12

user1888584


People also ask

How do I add jQuery to my HTML?

Step 1: Open the HTML file in which you want to add your jQuery with the help of CDN. Step 2: Add <script> tag between the head tag and the title tag which will specify the src attribute for adding your jQuery. Step 3: After that, you have to add the following path in the src attribute of the script tag.

How copy HTML using jQuery?

To clone an element using jQuery, use the jQuery. clone() method. The clone() method clones matched DOM Elements and select the clones. This is useful for moving copies of the elements to another location in the DOM.

How do you duplicate an element in HTML?

The cloneNode() method creates a copy of a node, and returns the clone. The cloneNode() method clones all attributes and their values. Set the deep parameter to true if you also want to clone descendants (children).

Can we write jQuery in HTML file?

Step 1: Firstly, we have to open that Html file in which we want to add the jQuery using CDN. Step 2: After then, we have to place the cursor between the head tag just before the title tag. And, then we have to use the <script> tag, which specify the src attribute for adding.


1 Answers

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?
like image 90
Andrew Avatar answered Sep 30 '22 04:09

Andrew