calling another events onclick ...

P

phish.guy

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...
 
W

web.dev

<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);
...
}
 
D

david sargent

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
 
R

Richard Cornford

david said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,830
Latest member
HeleneMull

Latest Threads

Top