Reputation: 44351
I have two form elements, a email
and a password
. The password
element must only be shown if something has been entered in the email
element. That is, only if email
is not empty.
<input id="email" name="email" onblur="if (jQuery("#email").val() === "") { jQuery("#password").hide(); } else { jQuery("#password").show(); }" onchange="if (jQuery("#email").val() === "") { jQuery("#password").hide(); } else { jQuery("#password").show(); }" onfocus="if (jQuery("#email").val() === "") { jQuery("#password").hide(); } else { jQuery("#password").show(); }" placeholder="Email" style="width:360px;height:28px;margin-bottom:0px;" type="text">
As you see I am handling three events:
onfocus
: so that I react when the focus enters the email elementonblur
: so that I react when the focus leaves the email elementonchange
: so that I react when the email element changesActually, I only want to run the script whenever the email changes. I do not care about focus, but I am unable to handle a generic change event.
Besides, and this is my main problem, I am not able to react to the the event generated when the browser is auto-filling the form. Is there such an event? How can I handle it?
Upvotes: 1
Views: 899
Reputation: 7132
Browser auto-filling occurs on initial page rendering. Have you tried checking the field in an "onReady" callback? Note that this event is only provided by higher level implementations like Jquery, not directly by the browser.
Example of "ready" callback with jquery: (updated to reflect the comments)
$(function() {
setTimeout(function() {
$("#email").trigger('change');
}, 200);
});
There is also an article explaining the "do it yourself way": http://dustindiaz.com/smallest-domready-ever
Upvotes: 1
Reputation: 21386
You may try disabling the browser autocomplete feaure by;
<form action="demo_form.asp" method="get" autocomplete="off">
or
<INPUT NAME="name" SIZE=40 AUTOCOMPLETE=OFF>
Note. The first one is HTML5 <form>
autocomplete Attribute
Upvotes: 1