First Time Plugin Coding/ Tutorial Wanted
Oct 20, 2013 18:35:53 GMT -8
Chris, Wormopolis, and 1 more like this
Post by Jordan on Oct 20, 2013 18:35:53 GMT -8
Just bear with me, this explanation is going to be a little detailed.
JQuery is a very robust Javascript library, so it will handle a lot of developer mistakes gracefully so it doesn't crash and error out. If you look at the description of the html() method on JQuery's website, you'll see that there are three different definitions:
html()
html( htmlString )
html( function(index, oldhtml) )
The first returns the element's html so you can see it, the second (what you are using) replaces the element's html to whatever you want, and the third allows you to define a function that gets executed on each item the method is called on. Since you were using the second definition, JQuery expects a string of text. However, in Javascript, you can pass any type of variable or object to any function, and it's up to the programmer to make sure that what you passed is acceptable. In this case, rather than passing a string, you passed a JQuery object. The reason it still worked is because the JQuery developers actually coded the html( htmlString ) method to handle the case where a JQuery object gets passed instead and have it converted to html. They did some extra work.
Now, the reason why stat_str didn't appear is because the JQuery developers did some extra work for you again. You created an invalid JQuery object, because you can't create a JQuery object with a random string. If you pass a string to a JQuery object, it needs to be an html element or some type of selector. The <br /> is an html element that it was able to parse and understand, but the stat_str was just a random string. It wasn't an html element so it ignored it for you, rather than give an error.
Hope that makes sense, but if not, let me know.
Also, one quick note about JQuery that I'd like to mention since I showed you html( function(index, oldhtml) ) above: You actually don't have to call each(). Your code could actually be shortened to this:
The thing to realize is that when you use a selector like $(".mini-profile div.info"), it contains all of the elements that match that description on the page, and any method you call on it will effect all of those elements. We use each() if we want to treat some of the element's differently. In your case, you are treating them all the same by adding "<br />Hello World", so you actually don't need to use each(). The html( function(index, oldhtml) ) method is just like each() but more specific for html scenarios. The function you pass to it will be called on each item that was selected, and it basically allows you to do different things to each of the selected elements. If you wanted to do the same thing to all the elements, then you would just use html( htmlString ).
JQuery is a very robust Javascript library, so it will handle a lot of developer mistakes gracefully so it doesn't crash and error out. If you look at the description of the html() method on JQuery's website, you'll see that there are three different definitions:
html()
html( htmlString )
html( function(index, oldhtml) )
The first returns the element's html so you can see it, the second (what you are using) replaces the element's html to whatever you want, and the third allows you to define a function that gets executed on each item the method is called on. Since you were using the second definition, JQuery expects a string of text. However, in Javascript, you can pass any type of variable or object to any function, and it's up to the programmer to make sure that what you passed is acceptable. In this case, rather than passing a string, you passed a JQuery object. The reason it still worked is because the JQuery developers actually coded the html( htmlString ) method to handle the case where a JQuery object gets passed instead and have it converted to html. They did some extra work.
Now, the reason why stat_str didn't appear is because the JQuery developers did some extra work for you again. You created an invalid JQuery object, because you can't create a JQuery object with a random string. If you pass a string to a JQuery object, it needs to be an html element or some type of selector. The <br /> is an html element that it was able to parse and understand, but the stat_str was just a random string. It wasn't an html element so it ignored it for you, rather than give an error.
Hope that makes sense, but if not, let me know.
Also, one quick note about JQuery that I'd like to mention since I showed you html( function(index, oldhtml) ) above: You actually don't have to call each(). Your code could actually be shortened to this:
function display_stats()
{
$(".mini-profile div.info").append($("<span></span>").html("<br />Hello World"));
}
$(document).ready(function() {
display_stats();
});
{
$(".mini-profile div.info").append($("<span></span>").html("<br />Hello World"));
}
$(document).ready(function() {
display_stats();
});
The thing to realize is that when you use a selector like $(".mini-profile div.info"), it contains all of the elements that match that description on the page, and any method you call on it will effect all of those elements. We use each() if we want to treat some of the element's differently. In your case, you are treating them all the same by adding "<br />Hello World", so you actually don't need to use each(). The html( function(index, oldhtml) ) method is just like each() but more specific for html scenarios. The function you pass to it will be called on each item that was selected, and it basically allows you to do different things to each of the selected elements. If you wanted to do the same thing to all the elements, then you would just use html( htmlString ).