Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    GetElement of event value

    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

  • #2
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    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).
    Code:
    <!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.
    Last edited by itsallkizza; 01-06-2009 at 05:14 AM.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #3
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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()

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,366
    Thanks
    11
    Thanked 590 Times in 571 Posts
    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.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #5
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    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:
    Code:
    <!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>
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • Users who have thanked itsallkizza for this post:

    maheshdasari (01-06-2009)

  • #6
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up

    Thank you very much, it works for me


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •