Main content
Computer programming
Course: Computer programming > Unit 7
Lesson 3: DOM modification with jQueryReview: DOM modification with jQuery
Once you have found a collection of elements using the jQuery function, you can change them using various methods.
Set attributes with
attr()
:$(".dog-pic").attr("src", "dog.jpg");
$(".google-link").attr("href", "http://www.google.com");
(See example)Change CSS styles with
css()
:$("h1").css("font-family", "monospace");
$("h1").css({"font-family": "monospace", "color": "red"});
(See example)You can also create new elements from scratch by passing a string of HTML into the jQuery function:
var $p = $("<p>");
If you'd like, you can pass in the full HTML, including tag contents, attributes, and styles.
var $p = $('<p style="color:red;">I love people who love cats.</p>');
Once you create an element, you can modify it using any of the methods above:
$p.addClass("warning");
Then you can append it to an existing element using
append()
:$("#main-div").append($p);
(See example)Read the jQuery documentation for more details on each of these methods, following the linked method names above. We won't be able to cover everything here, and as web development best practices change quickly, you will need to get good at reading documentation to keep up to date on the latest and greatest. It's good to start working on that skill now!
Want to join the conversation?
- Is there any reason in particular to use
$.appendTo();
over$.append();
? I realize the order of the arguments you supply is flipped in$.appendTo();
, but is there any real reason to use it over$.append();
?(15 votes)- More often than not, you've just called a method or two on the jQuery collection you are appending. Using
appendTo()
, you can simply chain all the methods like so:$("#my-image").width(300).appendTo(body);
(23 votes)
- Is there an $.animate(); ?(3 votes)
- Yes, here's the tutorial section on DOM animation and effects with jQuery:
https://www.khanacademy.org/computing/computer-programming/html-js-jquery/dom-animation-with-jquery/p/animating-visibility-with-jquery(5 votes)
- can you please explain to me the simple difference between append and appendTo?(3 votes)
- The append (content) method appends content to the inside of every matched element, whereas the appendTo (selector) method appends all of the matched elements to another, specified, set of elements.
I hope this helps! =](2 votes)
- Hi!
I am using Chrome and I'm not sure how to add jQuery in the DevTools. I've modified a few colors and texts in the past but I've never added a completely new line of code before. Can someone please help? Thanks!😀(3 votes)- Hi! Refer to this tutorial on introduction to JQuery. It also tells how to implement JQuery in your own projects. https://www.khanacademy.org/computing/computer-programming/html-js-jquery/jquery-intro/pt/getting-started-with-jquery(1 vote)
- Why do you not need to use a ; at the end of jQuery command?(2 votes)
- Because you don't have to use it for JavaScript either. It's just for line-breaks, parsing errors, and compiling (in other languages). But you should still use it.(3 votes)
- In the 6th paragraph from the bottom, the method of creating a new element is to declare a variable called "$p". Previous talk-throughs indicated "$" is equivalent to typing "jQuery" as a token to indicate you're accessing the jQuery library. However, in the "Creating elements with jQuery" talk-through and the "Creature creator" challenge, the name of the variable created didn't start with "$" (e.g., the talk-through used "newP").
Is there a reason "$" is used in the variable name in this review?(2 votes)- I believe the choice to use "$" in front of the variable name is just a convention many people use for objects which were created using JQuery and which therefore hold a JQuery object collection. Any name can be used for the new element. It's up to the programmer.(2 votes)
- The function name "addClass" always makes me confused. That function name sounds as if you're creating a new class.
;| which is really why I wish it were called "addToClass", to help be more clear.
dont flag, question comin' right up
Is there a specific reason for doing so, does it actually create a class in a way.. or is my point of view correct?(2 votes)- The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute ;|(2 votes)
- Why would you change a paragraph in jquery instead of just making the paragraph the way you want it?
And same for adding paragraphs cant you just add one without jquery?(1 vote)- Hi William,
When I click "Reply" under your question, JavaScript creates a text area where I can type my answer for you.
When I then click "Post reply", JavaScript takes that text I wrote and does two things.
1) It creates a paragraph with my text and places it under your question.
2) It sends the text to Khan Academy's servers, so it can save it in a database. That way, you can see my answer when you open this page.
In step 1, it would not have been possible to create that paragraph any other way than with JavaScript. Because no one knows what I'm going to write yet.
In step 2 when you load the page, they do know what I wrote. It's saved in the database, after all. So then your suggestion works well. And that's more or less what they do.(4 votes)
- Why would people use jquery to change the DOM rather than innerHTML to change the DOM?(1 vote)
- Many people use jQuery because it’s efficient and easy to learn. It also has great cross browser support.(2 votes)
- If I use appendTo, will the element go to the end of the target, just like if I use append?(1 vote)
- append is used for when you have the position, and not the item.
appendTo is used for when you have the item, and not the position.
append is used for when you have the position, and not the item, then you put the item in the brackets.
appendTo is used for when you have the item, and not the position, then you put the position in the brackets.(2 votes)