Tukhsanov
Tukhsanov

Reputation: 303

Differences using $(). jQuery

I want to ask what's the differences using with $() or without $(). In two cases actually it is working. Is there any differences i really not understand.

jQuery

var harf = $('<p>Hello there</p>').appendTo('body');
  $(harf).on('click', function(){
    alert('harf');
  });

//

var harf = $('<p>Hello there</p>').appendTo('body');
  harf.on('click', function(){
    alert('harf');
  });

Upvotes: 1

Views: 66

Answers (1)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382112

The first one is just totally useless. There's no point in embedding a jQuery object in a jQuery object.

Note that you could chain the calls, you don't really need that harf variable :

$('<p>Hello there</p>').on('click', function(){
    alert('harf');
}).appendTo('body');

Upvotes: 5

Related Questions