Main content
Computer programming
Course: Computer programming > Unit 7
Lesson 4: jQuery techniques- jQuery Collections vs. DOM Nodes
- Using jQuery variables
- Looping through jQuery collections
- Challenge: Loopy language
- Review: jQuery collections & looping
- Chaining jQuery methods
- Challenge: Daisy chain
- Behind the scenes: Browse the jQuery source code
© 2023 Khan AcademyTerms of usePrivacy PolicyCookie Notice
Behind the scenes: Browse the jQuery source code
You might be thinking jQuery is pretty magical at this point, with everything it can do. But it's not magic, it's just a whole lot of JavaScript - and since it's open source, anybody can browse the code of jQuery and see the magic for themselves.
You can see the source on Github, or use these two sites that make it easier to browse through: jQuery source viewer and jQuery deconstructed.
For example, take a look at the code for the
html()
method. It does a lot of checks for various error conditions and edge cases and eventually, it runs the line of code that actually tells the browser to set the inner HTML contents:elem.innerHTML = value;
It can be intimidating to look at the code for jQuery, because it involves both advanced JavaScript syntax and a whole lot of expert knowledge about how browsers work - but it can also be really cool to see all the code that goes into making a powerful JS library that works across so many browsers.
Want to join the conversation?
- Since jQuery is open source, can anyone such as me edit it? If so, where and how could we do this?(19 votes)
- You can indeed. jQuery has a repository on GitHub, which contains loads a issue reports, which you could try and tackle, before finally sending them a pull-request.
Here's the link, if you're interested: https://github.com/jquery/jquery(27 votes)
- worked!
for (var i = 0; i < 5; i++ ) {
$("<img>", {
src: "link",
alt: "text",
width: "number",
}).appendTo("body");
}
not worked (: why?
var image = $("<img>").attr({"src" : "link", "alt" : "text", "width" : "number"});
for (var i = 0; i < 5; i++) {
$(image).appendTo("body");
}(5 votes)- In the first time, you ran through the loop five times, each time creating a new image and appending it to the body. In the second time, however, you created an image and added it five times to the body. You added the same image to the body again and again- which resulted in the single image being added to the body.(10 votes)
- On the previous challenge what did I do wrong:
$("<img>").src("https://upload.wikimedia.org/wikipedia/commons/2/29/English_Daisy_(Bellis_Perennis).jpg").img-width("300").alt("daisy").appendTo("body");
Oh, I GOT it! Please let me know if this code can be improved:
$("<img>").attr("src", "https://upload.wikimedia.org/wikipedia/commons/2/29/English_Daisy_(Bellis_Perennis).jpg").attr("width", "200").attr("alt", "daisy").appendTo("body");(3 votes)- You could add new lines between all the .whatevers, for example
$("<img>")
.attr("src", "https://upload.wikimedia.org/wikipedia/commons/2/29/English_Daisy_(Bellis_Perennis).jpg")
.attr("width", "200")\
.attr("alt", "daisy")
.appendTo("body");(8 votes)
- Finally it worked -the daisy Chain challenge
$("<img>").attr("src", "https://upload.wikimedia.org/wikipedia/commons/2/29/English_Daisy_(Bellis_Perennis).jpg")
.attr("alt", "daisy photo")
.attr("width", "250")
.appendTo("body");
for(i= 0; i<4; i++){
$("<img>"+i).attr("src", "https://upload.wikimedia.org/wikipedia/commons/2/29/English_Daisy_(Bellis_Perennis).jpg")
.attr("alt", "daisy photo"+ i)
.attr("width", "250")
.appendTo("body");
}(5 votes) - How would you create something like this library and edit it anytime by anyone?(4 votes)
- A library is simply a JavaScript object containing lots of useful methods that work together. In jQuery's case, this is
$
(orjQuery
). Just wrap your library into a single object and you're generally good to go.
There are lots of online services out there where programmers can collaborate on projects together. The most popular one right now is GitHub (https://github.com), a code repository service. Much like KA's spin-off system, you can make "forks" of another person's project. You can then make a "pull request", or a suggestion to the original author to merge your edits into their original project. It's a highly efficient and productive way to collaborate.
jQuery and Khan Academy use GitHub:
https://github.com/jquery/jquery
https://github.com/khan(3 votes)
- Hello guys, kindly can anyone help me out with this one please. I am a little stacked, i can't figure out what this sentences actually mean...
"send its text through the toPigLatin function, and set the paragraph to that text."
from the loopy Language Challenge.(3 votes)- The sentences are the paragraphs. When you do your $("p") query, it will returns you all the query proprieties to use with that paragraph. If you want the text propriety, you can just do the $("p").text(), as explained.
The toPigLation function accepts only text, so, when passing this argument, you should pass it as a text. Now, getting to the "send its text through the toPigLatin function, and set the paragraph to that text.", the text referred is the return of the function, so you will end up with something like... $("p").html(toPigLatin($("p")), and I've said "something like" because you have all the element stuff going on, remember that :)(1 vote)
- What am I supposed to put inside the
for
loop in the previous challenge?(2 votes)- You should put all the code which you have written in the first step inside the for loop...hope it helps! by the way, I just read it today only aaand its been two years.(1 vote)
- How do I use for loop on the 2nd part of the previous challenge? This is what I have:
for (var i=0; i<5; i++) {
$newIMG.appendTo("body");
};
But it's not working. Ughhh(1 vote)- What's not working? is it not appending the image to the body? Maybe you haven't defined the image properly? I don't see the rest of the code so I can't say.
Were you expecting 5 images? Because you only have a single image. You're simply placing it into the body 5 times, which is pretty useless. To put 5 images you need to create 5 image elements, and not add the same element multiple times.(1 vote)
- I don't get what to do in "Challenge: Daisy chain". The second step is going missing for some reason. Do you know what the reason is?(1 vote)
- Try this:
for (var i = 0; i < 24; i++) {
$("<img>")
.attr("src", "https://upload.wikimedia.org/wikipedia/commons/2/29/English_Daisy_(Bellis_Perennis).jpg")
.attr("width", "90")
.attr("alt", "A picture of a daisy")
.appendTo("body")
}(3 votes)
- Hi guys can someone help me with what this line of sentence mean, my daisy challenge contains a chain.. but i just don't understand what my teach want me to do here.... any help pls?
Does your page have a daisy chain on it now? Celebrate by taking a walk and making a real daisy chain!(2 votes)