I am writing my own "class" functions in JavaScript so I can call it and it will format a form how I want it.
The thing is i wrote my TextFade function in a website where I had access to jQuery but i would like my own code to be stand alone (not reliant on jQuery)
My question is how can I convert this piece of code :
this.textFade = function(element,password){ // needs to be changed so jQuery is not needed anymore
if(password === 'undefined')
password = false; // default
$(element).focus(function() {
if( this.value == this.defaultValue )
this.value = "";
if(password == true)
this.type = "password";
}).blur(function() {
if( !this.value.length ) {
this.value = this.defaultValue;
if(password == true)
this.type = "text";
}
});
}
To something that doesnt require jQuery.
The main problem I am having is how I can't check what event got triggered on the element, if someone can explain that to me I would be able to fix it myself (without using another parameter to define it if possible).
Tried to find it in jQuery but I could not find the function for some reason.
I usually prefer using native javascript, but this is a case where jQuery really does come in handy. The biggest problem is you are passing in element, which could be anything (classname, node, id, tagName) which is handled by jquery quite nicely. To simplify things, we will switch
element
with anid
parameter, and get the node withdocument.getElementById
.As opposed to extending jQuery, you can use the singleton pattern and extend your own objects. Set an empty object (obj), and add functions to that.
I'm not 100% sure of the functionality you are after, but here a basic translation.
Here is a live version: http://jsfiddle.net/CJ6DK/1/
Normally you'd know which event was triggered because you hooked up a function to a specific event. But if you hook the same function up to more than one event, you can still determine which event occurred:
When you hook up an event using
addEventListener
, your event handler will receive anEvent
object. The type of the event is available from that object'stype
property. So for instance:Not all browsers currently in use in the wild support
addEventListener
, however (this is one of the several reasons to use a library like jQuery). On older versions of IE, you may need to useattachEvent
instead (you can check by simply looking to see if the element has anaddEventListener
property on it). Note that if you do, within the handler,this
won't point to the element on which you hooked the event (it'll point towindow
).If you assign a function to the
onxyz
property of the element (e.g.,elm.onclick = function...;
), you won't receive the event object as an argument. On some browsers (IE, Chrome) you can find the event on thewindow
object. That event object is very similar to the ones passed in byaddEventListener
orattachEvent
, so you see this in handlers that might get hooked up that way:...so that if there was no argument passed to the function (e.g., it wasn't hooked up with
addEventListener
orattachEvent
, but was assigned directly to the property), it will usewindow.event
rather thanevent
.