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 Kurnia
http://hantulab.blogspot.com
http://www.plurk.com/user/donnykurnia


Reply via email to