Why jQuery cached variable is not seen in an outer function? -


i curious why code not working.

i can't see $note variable in hey() function

function hey(kc) { $note.html(kc);  }  $(function () {   var $note = $('#note');      hey("joice");   }); 

a fiddle play http://jsfiddle.net/vcdxb/

as others have said, it's matter of scope. pattern caching jquery objects:

// global scope // single hash hold reference cached jquery objects var $jq = {};  $(function() {     $jq.note = $('#note');     $jq.name = $('#name');   });  function hey(kc) {     $jq.note.html(kc); } 

Comments