...

View Full Version : GetElement of event value



maheshdasari
01-06-2009, 04:53 AM
Hi,

Is there a way to getelementbyid("textid").onchange function value.

For ex:

<head>
function fieldsevaluate(){
document.getElementById("firstname").onchange // returns fucntion event()
// is there a way to get changefn(); by anymeans
}
</head>
<input type="text" name="firstname" value="" onchange="changefn()" />

Thanks in Advance,
Mahesh Dasari

itsallkizza
01-06-2009, 05:11 AM
You have the right idea, but remember: the element in question needs to have an id attribute for document.getElementById() to work cross-browser (not just a name).

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Example</title>
<style type="text/css">
</style>
<script type="text/javascript">
// <![CDATA[

window.onload = function()
{
var display = document.getElementById("display");

//get function with id of input
display.innerHTML += document.getElementById("my_input").onchange;

//get function with id of parent form and input name
display.innerHTML += "<br /><br />"+document.getElementById("my_form").my_input.onchange;
}

// ]]>
</script>
</head>
<body>

<form id="my_form" onsubmit="false">
<input id="my_input" type="text" name="my_input" onchange="function(){alert('hey!');var this_var_is_important = 'important value';}" />
</form>

<br /><br />
Output:
<div id="display" style="font-size:10px;font-family:arial,sans-serif;"></div>

</body>
</html>
Of course you don't necessarily need ids or names to reference objects, just a pointer (reference variable) of some kind. The above are just examples.

maheshdasari
01-06-2009, 05:18 AM
Thanks for your quick response,

Well i want to get the function name that was used at onchange,

<head>
function fieldsevaluate(){
document.getElementById("firstname").onchange // returns fucntion event()
// is there a way to get changefn(); by anymeans
}
</head>
<input type="text" id = "firstname" name="firstname" value="" onchange="changefn()" />

document.getElementById("firstname").onchange always returns
function anonymous{
changefn()
}

The one i am expecting is changefn()

rnd me
01-06-2009, 06:21 AM
the quick answer is no.

firefox provides function.name, but your function is wrapped in it's anon, meaning you would have to toString() it, and poke around the function body text to try to find the function call. all that, and it only would work for firefox...

perhaps you can use a global or an object property to track the function easier than trying find it later.

itsallkizza
01-06-2009, 06:22 AM
I coded this up for you if you're just looking for the string name. I don't see the purpose lol, but here ya go:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Example</title>
<style type="text/css">
</style>
<script type="text/javascript">
// <![CDATA[

function changefn()
{
fn = "something else entirely";
}

function extractInnerFunctionString(event_name,element)
{
var en = event_name.substr(0,2).toLowerCase() == "on" ? event_name : "on"+event_name;
if (element[en] && element[en].toString)
{
var full = element[en].toString();
return full.substring(full.indexOf("{")+1,full.indexOf("}")).replace(/\n/g,"").replace(/\t/g,"").replace(/ /g,"");
}
return "";
}

window.onload = function()
{
var function_name = extractInnerFunctionString("change",document.getElementById("my_input"));
alert(function_name);
}

// ]]>
</script>
</head>
<body>

<form id="my_form" onsubmit="return false">
<input id="my_input" type="text" name="my_input" onchange="changefn();" />
</form>

</body>
</html>

maheshdasari
01-06-2009, 07:16 AM
Thank you very much, it works for me :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum