Jump to content

When Creating JQuery Functions..


Deji

Recommended Posts

I don't know much Javascript and am not gonna bother tryna learn it in a hurry, but JQuery seems nice, fast and practical. It's also almost as easy as BB Code and easier than XHTML, so I'm definitely gonna use it. Granted, I could read a tutorial, but jumping in has more of a lasting effect on me.At the moment, I'm working on creating plenty of nice functions for my website upgrade. The functions work already, but I'm wondering whether I'm going by the proper things to do when creating functions with JQuery.For example, I was creating some simple show/hide functions before I started wondering (which lead to this topic).

function hideme(){$(document).ready(function(){$(this).hide()});}function showme(){$(document).ready(function(){$(this).show()});}function hide(selector){$(document).ready(function(){$(selector).hide()});}function show(selector){$(document).ready(function(){$(selector).show()});}function fancyhide(selector){$(document).ready(function(){$(selector).hide(750)});}

This $(document).ready thing.. Do I have to use it on each function or would there be a better way to do this (so I don't have to repeat it every single time)? Feels like I should be able to put that thing around the whole block of functions, but my mental logic says "no"... of course, this is Javascript...Do I use normal Javascript functions if I want to call them from stuff like onclick events and pass parameters or does JQuery have a method for doing this itself?.. I coulda sworn I had another question which was more worthy of this topic..In a nutshell.. Am I doing this right? It works, but is it "valid"? (what's with nutshells anyway.. how is that equivalent to saying "to sum up.."? the only type of nuts I like with a shell are pistachios.. wow, this is a big nutshell, hope the nuts not salted or the eater might get a heart attack).I read the W3Schools tutorial on JQuery, but while it constantly says how to have a script in the header to run when ___ element is clicked etc. - it said nothing about calling functions and passing params through JQuery. That I figured out for myself... And I'm handling with lots of multiple "click to show", "click to hide" things dragged from the database by PHP, with about 50 different ID's per page. Selectors alone just don't work here.After mastering this simple show/hide thing, it's straight to making inline-editable divs...EditOh yeah.. my "hideme" and "showme" functions don't work. I guess "this" isn't getting the thing that called the function. This is where JQuery-specific functions would come in useful.

Link to comment
Share on other sites

There is a much better way to do this.

function hideme() { $(this).hide(); }function showme() { $(this).show(); }function hide(selector) { $(selector).hide(); }function show(selector) { $(selector).show(); }function fancyhide(selector){ $(selector).hide(750); }

You only need to worry about $(document).ready when you are calling the functions. the ready event is like window.onload (except ready won't wait for images and external resources to load). It happens when the document first loads. You only want to use it once on the page. You could call the functions like this.

$(document).ready(function() {  hide('.hideables');  //etc. etc.});

jQuery does not have functions. jquery is a framework build with JavaScript that essential makes selecting elements easy and abstracts away some of the crossbrowser pain. jquery has it's own way of defining events like clicks.

$('#myid').click(function() {  //do something here});

I would recommend reading the jQuery tutorials and documentation.

Link to comment
Share on other sites

Thanks. I figured JQuery might have some sort of "listener" in order to have its own functions.I just wanna use functions to make sure it won't mess up the page for those without Javascript enabled.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...