I have an HTML document.
It is possible to get the events associated with every Element in a particular FORM element in the document using javascript.
var element = document.forms[i].elements[j];
This way I can get j th element in i th form, But can I get the event associated with the element.
There can be any number of elements in a form.I am using IE 8.
Thanks
EDIT :
Actually I was trying to serialize HTML DOM into XML.
what I did to do this was :
createXML : function() {
objSerializeDOM.msg += "";
objSerializeDOM.msg += "<?xml version='1.0' encoding='UTF-8'?>\n\n";
// Get all the forms in a document.
var forms = document.forms;
for ( var i = 0; i < forms.length; i++) {
// Get all the elements on per form basis.
elements = document.forms[i].elements;
objSerializeDOM.msg += "<FORM name=\"" + forms[i].name + "\" method=\""
+ forms[i].method + "\" action=\"" + forms[i].action + "\">\n\n";
for ( var j = 0; j < elements.length; j++) {
objSerializeDOM.msg += " <" + elements[j].tagName + " type=\""
+ elements[j].type + "\"" + " name=\""
+ elements[j].name + "\"" + " Value =\""
+ elements[j].value + "\" />\n";
}
alert(document.forms[i].elements[1].event);
}
objSerializeDOM.msg += "\n\n</FORM>\n\n";
alert(objSerializeDOM.msg);
objSerializeDOM.writeToFile(objSerializeDOM.msg);
}
What I am getting from this is an XML :
<?xml version='1.0' encoding='UTF-8'?>
<FORM name="loginForm" method="post" action="/sigma/login.do;jsessionid=E6509E7BA55573AA5386274ABB93F718">
<INPUT type="hidden" name="message" Value ="" />
<INPUT type="hidden" name="userAction" Value ="" />
<INPUT type="text" name="userId" Value ="" />
<INPUT type="password" name="passwd" Value ="" />
<INPUT type="button" name="button" Value ="Continue" />
</FORM>
Now Suppose I have :
<input tabindex="7" name="button" type="button" class="button"
style="width:100;height:30" Value="Continue" onclick='login()' />
All I want to do now is to get onClick in my XML or any event associated like onBlur() etc.
Thanks
Let's say you wanted to find all the event listeners attached to the search icon DOM element. Right-click on the search icon button and choose “inspect” to open the Chrome developer tools. Once the dev tools are open, switch to the “Event Listeners” tab and you will see all the event listeners bound to the element.
The easiest way to access a single element in the DOM is by its unique ID. You can get an element by ID with the getElementById() method of the document object. In the Console, get the element and assign it to the demoId variable. Logging demoId to the console will return our entire HTML element.
The addEventListener() method You can add many event handlers of the same type to one element, i.e two "click" events. You can add event listeners to any DOM object not only HTML elements. i.e the window object. The addEventListener() method makes it easier to control how the event reacts to bubbling.
As Felix said in his comment, there are several ways to register an event on an object. So how you can get the events attached to an object and serialize them somehow to your xml depends on how they are registered. I will list some thoughts of how you can get them for serialisation.
1 Handlers registered inline
1.1 Completely inline code:
<INPUT type="hidden" name="message" Value ="" onclick="alert('hello')"/>
When the code is completely inline, you can just get the attribute in your xml and save it.
2.1 Inline function call
In this case you would have to export the declaration of the function. In JavaScript Functions are objects itsself so you could actually get the declaration text of your function by invoking myFunc.toString()
. The hard part on this would be to figure out, if this is a function call where the declaration hast to be exported or not.
2 Handlers registered through attributes
If you have added all your element Handlers through i.e. :
function myFunc(num){
alert("Your number is: " + num);
}
document.getElementById('myElement').onclick = myFunc;
you could just iterate your form elements like you already do and get the onlick, onmouseover, onblur, on.... properties all one by one and save them to xml. Also in this case the content of this propertys will be Function Objects as well so to save their actual content you have to do .toString()
on the Function object.
In addition there are some other ways to register Event handlers depending on the different Browsers. So if you definetly know how your events are registered, you can actually serialize them. If you don't that's going to be very difficult.
I hope that helps to get you a bit further.
This probably isn't what you're looking for but may help you atleast see which part of the DOM you need to be looking at - you can get a plugin for firebug which shows any jQuery events bound to DOM elements called firequery, and I think firebug on its own can show attached events in normal JS.
Considering that firebug is written in JS, there obviously must be a way to do it. Unfortunately I don't have time to go through the source myself (I'd like to :D ) but you can find the repo here: http://code.google.com/p/fbug/source/browse/branches/firebug1.8/content/firebug/
Sorry I can't be of more help and good luck
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With