This article is currently in the process of being translated into Vietnamese (~22% done).


Hello, world!

Every decent programming tutorial will start with a "Hello, world!" example and this tutorial is yet another one of them. In the previous chapter, we learned how to include jQuery on our page, so that we may start using all of its great features. You need to know a bit more about how jQuery works, before you can start writing your own code, but just to make sure that everything is working, and for you to see how simple jQuery is, let's kick off with a little example:

<div id="divTest1"></div>
<script type="text/javascript">
$("#divTest1").text("Hello, world!");

Vậy chúng ta có thẻ div với id là "divTest1". Trong mã JavaScript ta dùng dấu $ để truy cập, có nghĩa là chọn toàn bộ element có id là "divTest1" (chỉ có 1 trong ví dụ) và thiết lập "Hello, world!". Bạn có thể không biết cụ thể cách hoạt động của jQuery nhưng trong bài giảng này, toàn bộ element sẽ được giải thích cụ thể.

Even such a simple task as this one would actually require quite a few extra keystrokes if you had to do it in plain JavaScript, with no help from jQuery:

<div id="divTest2"></div>
<script type="text/javascript">
document.getElementById("divTest2").innerHTML = "Hello, world!";

And it would be even longer if our HTML element didn't have an ID, but for instance just a class.

Normally though, you should wait for the document to enter the READY state before you start manipulating its content. The above examples will work in most browsers and likely even work when you do more advanced stuff, but certain tasks may fail if you try to do them before the document is loaded and ready. Fortunately, jQuery makes this very easy as well, as we will see in the next chapter. After that, we will start looking into one of the most important aspects of jQuery, which has already been used in the above example: Selectors.

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!