Thanks, T.J.

That looks like what I'm need to solve the issues.

I'll give it a whirl!

Rick

-----Original Message-----
From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On
Behalf Of T.J. Crowder
Sent: Sunday, April 05, 2009 1:07 PM
To: jQuery (English)
Subject: [jQuery] Re: Is there a way to use this function without clicking
the link twice?


Hi Rick,

> Question...I've begun to use named functions so that
> I can target them from multiple locations.  Kind of like
> the old subroutines I programmed 29 years ago in BASIC.
>
> Is there a way to be able to target them besides using
> "onClick" and remaining unobtrusive?
> ...
> Using my typical approach of firing a function based
> on $('#myID').click(function(){   requires a section
> of code for each id or class or whatever that is clicked.

You can use named functions anywhere you can use anonymous functions.
So for instance, these two bits of code are nearly identical:

* * * * Using anonymous function:
$('#myid').click(function() {
    // ...do stuff here...
});
* * * *

* * * * Using named function:
function someClickHandler() {
    // ...do stuff here...
}

$('#myid').click(someClickHandler);
* * * *

I say "nearly" identical because one of them declares an identifier
for the function, and the other doesn't.

So if you want to define your handlers, then hook them up at document
load time:

* * * *
function someClickHandler() {
    // ...do stuff here...
}

$(document).ready(function() {
    $('#myid').click(someClickHandler);
});
* * * *

Naturally, if you want your document ready init code in a named
function, you can do that too.

FWIW, wrt your comment about writing BASIC subroutines:  You can
organize your functions in JavaScript to a very significant degree,
they don't all have to be globals.  JavaScript provides very powerful
OOP features in the form of prototypical inheritance.  You can also
simulate class-based inheritance if you like, although JavaScript is
not class-oriented; it's that flexible. :-)

HTH,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available


On Apr 5, 3:18 pm, "Rick Faircloth" <r...@whitestonemedia.com> wrote:
> Thanks for the info, Donny!
>
> Perhaps as my coding technique advances with
> jQuery and, especially AJAX, I'll find better ways
> to streamline function use.
>
> Question...I've begun to use named functions so that
> I can target them from multiple locations.  Kind of like
> the old subroutines I programmed 29 years ago in BASIC.
>
> Is there a way to be able to target them besides using
> "onClick" and remaining unobtrusive?
>
> Using my typical approach of firing a function based
> on $('#myID').click(function(){   requires a section
> of code for each id or class or whatever that is clicked.
>
> How can I set up my code so that I can target a named function
> without reference to the triggering code, such as with "onClick"
> which is embedded within the triggering code, itself?
>
> Thanks,
>
> Rick
>
>
>
> -----Original Message-----
> From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On
Behalf Of Donny Kurnia
> Sent: Sunday, April 05, 2009 9:44 AM
> To: jquery-en@googlegroups.com
> Subject: [jQuery] Re: Is there a way to use this function without clicking
the link twice?
>
> Rick Faircloth wrote:
> > Strange question, I know…and perhaps stranger coding, but…
> > I’m trying to trigger a function with a text link and it works, but with
the
> > function coded as is, the link has to be clicked twice.
>
> > I’d like to keep the function coded starting with “function
> > ajaxCreateStoryTitle() {“
> > rather than “(document).ready(function() {“ for code organization
> > purposes, but using
> > “function… causes the text link to have to be clicked twice to fully
> > execute the function.
>
> > All this has to do with a method I’m trying to organize a lot of code
> > that’s needed to
> > create an ajax-driven, single-interface page.
>
> > Anyway…here’s the trigger link:
> > <a id=”createStoryTitleLink” class=”link”
> > onClick=”fnCreateStoryTitle();”>create title</a>
> > Clicking that link fire this function:
> > function fnCreateStoryTitle()  { ajaxCreateStoryTitle(); }
> > The function, “ajaxCreateStoryTitle();” starts like this:
> > -------------------
> > function ajaxCreateStoryTitle() {
> >      $(‘#createStoryTitleLink’).click(function() {
> >           titleValue =
> > $(this).parent().parent().find(‘#createStoryTitleInput)val();
> >           etc…
> > ------------------
> > Because of this coding, I have to click the text link above twice; once
> > to trigger the function
>
> > and the second time to trigger the
> > $(‘#createStoryTitleLink’).click(function() { line.
>
> > I need the “titleValue” line to obtain the value of the text input
> > “#createStoryTitleInput”.
>
> > Perhaps there’s a different way to get that value other than with the
> > “click” function?
>
> > There may be a better way to achieve code organization that what I’m
> > doing, but it’s just
> > my way of evolving my coding style using jQuery and AJAX.
>
> > Suggestions?  Another way to code the function “ajaxCreateStoryTitle()”
> > and still
> > be able to reference the titleValue in the input
> > “#createStoryTitleInput” ???
>
> > Thanks for taking time to read through this…
>
> > Rick
>
> If you insist to use onClick, then you don't need
> $(‘#createStoryTitleLink’).click(function() {
>
> }
>
> it will become like this:
> function ajaxCreateStoryTitle() {
>   titleValue =
$(this).parent().parent().find(‘#createStoryTitleInput)val();
>   etc…
>
> Anyway, personally I'm not like to use onclick. I like the unobtrusive
> way of jQuery, using either (document).ready(function() { or it short
> notation:
>
> jQuery(function($){
>   //DOM ready
>   ....code here.....
>
> });
>
> The above lines can appear multiple time. I like to write it near the
> element that need the code, so it keeps the readability and keep the
> code tidy.
>
> --
> Donny
Kurniahttp://hantulab.blogspot.comhttp://www.plurk.com/user/donnykurnia

Reply via email to