Recently, I am starting to do javascript and jQuery, and one thing that I constantly ask myself is when to use "$", I know what points to jQuery, but it does not always seem like that. I will give some examples:
These are the two scripts I wrote:
Firstly:
$(function() {
var newHTML = '<span style="font-size: 1.7em; text-align:center; line-height:50px;">Login</span>';
var oldHTML = '<span style="font-size: 32px; line-height: 18px;">+</span><span style="font-size: 14px; float: left;">Add to watchlist</span>';
$("a.bid-addwatchlist").hover(
function () {
(this).innerHTML = newHTML;
},
function () {
(this).innerHTML = oldHTML;
});
});
Second:
(function(){
$("#container a").click(function(){
if ($(this).html() == "Stop Listening")
{
$(this).html("Listen");
}
else if ($(this).html() == "Listen")
{
$(this).html("Stop Listening");
}
});
});
Why doesn't this work in the first script if I have $ before "this" and a second script is needed?
Note: I already looked here: When to use $ and when not to
But this answer was not complete enough.
source
share