calling another events onclick ...

Discussion in 'Javascript' started by phish.guy@gmail.com, Aug 11, 2006.

  1. Guest

    How do I call another event's onclick function from a button press? I
    have stored information dynamically created in a div onclick function
    that is not accessible directly by the button, but i need to call the
    onclick for the specific div.

    my code looks like this:

    <div id='field_1' onclick='divclick(this, *(field1 specific data*))'
    ....> ...</div>
    <div id='field_2' onclick='divclick(this, *(field1 specific data*))'
    ....> ...</div>

    <input id="doclick" type="button" value="value" onclick='nextMove()'/>

    now, either field 1 or 2 is 'active' so i keep that stored in
    javascript as

    var current; //lets say it is pointing to 'field_1'

    function nextMove()
    {
    var next= new Array();
    next= current.id.split('_');
    var num = next[1] * 1 ; //get string to int
    num++; //provides me with 2
    document.getElementById('field_' + num).click(); //DO CLICK on
    field_2 does not work

    }


    thanks in advance for any help...
    , Aug 11, 2006
    #1
    1. Advertising

  2. web.dev Guest

    wrote:
    > <div id='field_1' onclick='divclick(this, *(field1 specific data*))'
    > ...> ...</div>
    > <div id='field_2' onclick='divclick(this, *(field1 specific data*))'
    > ...> ...</div>
    >
    > <input id="doclick" type="button" value="value" onclick='nextMove()'/>
    >
    > now, either field 1 or 2 is 'active' so i keep that stored in
    > javascript as
    >
    > var current; //lets say it is pointing to 'field_1'
    >
    > function nextMove()
    > {
    > var next= new Array();
    > next= current.id.split('_');
    > var num = next[1] * 1 ; //get string to int
    > num++; //provides me with 2
    > document.getElementById('field_' + num).click(); //DO CLICK on
    > field_2 does not work
    >
    > }


    You must already have a function called divclick defined somewhere,
    thus all you need to do is call your function in your nextMove
    function. For example, if I were to go along with your divclick
    parameters, I would use them in the following manner:

    function nextMove()
    {
    var oCurrentField = document.getElementById("field_" + num);
    //grab your field specific data
    var fielddata = ...

    divclick(oCurrentField, fielddata);
    ...
    }
    web.dev, Aug 11, 2006
    #2
    1. Advertising

  3. Thanks for the quick response...maybe i am just missing something, but
    the problem is the specific data is retrieved from a database, so it
    would look like for example:

    onclick='divclick(this, 13253, unique, moreunique)'

    each one is unique and dynamically retrieved,
    how do i pull '13253' out of the onclick without doing lots of nasty
    parsing...is there an easy way to just pull these specific elements
    out...

    after i do: var oCurrentField = document.getElementById("field_" +
    num);
    alert(oCurrentField.onclick)

    produces the string function(){divclick(this, ...)} but i dont want to
    have to do nasty string functions if possible...and i dont want to just
    add these variables into the string of html tags as that would not be
    compliant....

    so if there is an easy way to take an onclick event and pull the
    attributes for it, how do i do it?

    thanks




    > You must already have a function called divclick defined somewhere,
    > thus all you need to do is call your function in your nextMove
    > function. For example, if I were to go along with your divclick
    > parameters, I would use them in the following manner:
    >
    > function nextMove()
    > {
    > var oCurrentField = document.getElementById("field_" + num);
    > //grab your field specific data
    > var fielddata = ...
    >
    > divclick(oCurrentField, fielddata);
    > ...
    > }
    david sargent, Aug 11, 2006
    #3
  4. david sargent wrote:
    > Thanks for the quick response...maybe i am just missing
    > something, but the problem is the specific data is
    > retrieved from a database, so it would look like for
    > example:
    >
    > onclick='divclick(this, 13253, unique, moreunique)'
    >
    > each one is unique and dynamically retrieved,
    > how do i pull '13253' out of the onclick without doing
    > lots of nasty parsing...is there an easy way to just
    > pull these specific elements out...
    >
    > after i do: var oCurrentField = document.getElementById(
    >"field_" + num);
    > alert(oCurrentField.onclick)
    >
    > produces the string function(){divclick(this, ...)} but i
    > dont want to have to do nasty string functions if
    > possible...and i dont want to just add these variables
    > into the string of html tags as that would not be compliant....


    If - alert(oCurrentField.onclick) - gives you the string of the event
    handling function - oCurrentField.onclick(); - would call that event
    handler as a method of the DIV element (which is how it would normally
    be called by the browser). The only issue that may exist when doing that
    is the handling of event objects, but your event handler does not employ
    the event so that is not relevant here.

    > so if there is an easy way to take an onclick event and pull
    > the attributes for it, how do i do it?

    <snip>

    There is no need, just call the function that already exists.

    Richard.
    Richard Cornford, Aug 11, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. giant food
    Replies:
    2
    Views:
    11,777
    Chris Jackson
    Dec 12, 2003
  2. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    356
    Chris
    Aug 30, 2005
  3. bob
    Replies:
    3
    Views:
    183
  4. Replies:
    2
    Views:
    253
  5. Replies:
    4
    Views:
    176
Loading...

Share This Page