/    Sign up×
Community /Pin to ProfileBookmark

How do i know if a text field has focus?

Hi,
i’m new at the list and a newbie with javascript and dhtml.
What i want is to know if a text field has focus(the cursor inside the field).

<script>
function verifyFocus(fieldId) {
if(the field of the field id has focus) {
alert(“The field has focus!”);
} else {
alert(“The field DOESN’T has focus!”);
}
}
</script>

<input type=”text” id=”name” onMouseOut=”verifyFocus(this)”>

How can i proceed with this if statement in order to get what i want?

Cheers!
Alexandre.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@David_HarrisonJan 11.2005 — Just do this:&lt;input type="text" id="name" onfocus="alert('Focus? Yeah I got some focus for you.')" onblur="alert('Pfft, whatever, didn't even want focus anyway.')"&gt;
Copy linkTweet thisAlerts:
@alexmartinsauthorJan 11.2005 — Ok, i'll tell you what i want...

I want an input text to have on background-color by default,

another background-color when maouse over,

another background-color when mouse out,

and onother background-color when on focus.

I did it by changing the class of the input text when each of these actions are performed, for example:

<input type="text" id="name" class="aClass1" onblur="ChangeClass('name', 'aClass2');" onmouseover="ChangeClass('name', 'aClass3');" onmouseout="ChangeClass('name', 'aClass4');">

Ok, when i pass the mouse over, it works, when i have the mouse out, it also works, but when i click inside the field, it works, but only while the mouse is over the field area, if i have the mouse out, it changes again. What i wnat is when i have focus, no matter where the mouse is, i want the background-color not to change.

How can i do that?

Regards.

Alexandre Martins.
Copy linkTweet thisAlerts:
@pj59Jan 11.2005 — Hello!

I think, the easiest would be to have a variable the value of which is toggled depending on whether the field has focus or not. Here is a little example:[code=php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<style type="text/css">
<!--
.aClass1{
background-color:yellow;
}
.aClass2{
background-color:blue;
}
.aClass3{
background-color:red;
}
.aClass4{
background-color:lime;
}
.aClass5{
background-color:maroon;
}
-->
</style>
<script language="JavaScript" type="text/javascript">
<!--
var blah=0;
function ChangeClass(theName, theClass){
document.getElementById(theName).className=theClass;
}
//-->
</script>
</head>
<body>
<input type="text" id="name" class="aClass1" onblur="blah=0;ChangeClass('name', 'aClass2');" onmouseover="if(blah==0)ChangeClass('name', 'aClass3');" onmouseout="if(blah==0)ChangeClass('name', 'aClass4');" onfocus="blah=1;ChangeClass('name', 'aClass5');">
</body>
</html>[/code]
Regards PJ
×

Success!

Help @alexmartins spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...