About

  • Combination of the words "Andri" and "Droid"
  • Sysadmin, developer and a student
  • Born in Reykjavík, Iceland
  • Studying in Aalborg, Denmark
  • Not the Google project

Twitter

  • would you buy an ARM powered ultraportable for a extra few hours of battery?
  • Breaking: Broadcom open sources wireless drivers on Linux....
  • A really nice collection of web development resources....

jquery

Calling your own functions from jQuery

I've been playing around with jQuery for a week or so, without any prior experience with Javascript. jQuery is absolutely brilliant and it's quite easy to do different things with it. However, when I started doing repetitive tasks, i wanted to create a function to do it; instead of repeating my code at multiple places.

This is where I had problems, because my 'updateTable' function had no idea what $() was and no jQuery functions worked after I started calling my 'updateTable' function from the jQuery context.

Well, today I got it to work by sending the jQuery object to my function.

Old function (does not work)

function updateTable () {
/* Call jQuery stuff (for example, change forms) */
}

$(function() {
$(document).ready(function() {
updateTable();
});
});

After spending a couple of days scratching my head and reading a bit about Javascript, this was 'obviously' a problem of my function not knowing the jQuery context or any of the objects inside it.

There is probably a smarter way of doing this; but I at least solved my problem by appending the jQuery object '$' to the function, as follows.