Jakob
Jakob

Reputation: 749

Having onFocus do several things

So I have this input tag <input ... onFocus=...> and what I want is for a warning popup to come up if somebody clicks to write in the input field. After the person has read the warning he/she closes the popup and can continue to write in the field. I have managed to implement the popup just fine but my problem is that I lose focus on the input field when the popup comes up so the next time I click the field the popup comes back again and im back on square one.

The system I work on uses php and javascript so and I cannot use anything other than that to solve the problem and my experience with both php and javascript is limited.

Upvotes: 1

Views: 150

Answers (3)

Piksel8
Piksel8

Reputation: 1428

This is very easy to do with jQuery using the one() function:

$('#foo').one('focus', function() {
  alert('This will be displayed only once.');
});

The advantage of using jQuery or a javascript framework is that it makes it very easy for you to write unobtrusive javascript.

Upvotes: 0

Clyde Lobo
Clyde Lobo

Reputation: 9174

When you close the warning , you can put the focus on the input again.

See a demo http://jsbin.com/ifice4/

Upvotes: 0

Riimu
Riimu

Reputation: 1437

Just make the onfocus run a function that checks via a variable if the alert has already been shown. I assume you don't want to show the alert more than once per page view, so something like this would work:

<script type="text/javascript">
var focusShown = false;

function showFocusAlert ()
{
    if (!focusShown)
    {
        alert("You are about to focus!");
        focusShown = true;
    }
}
</script>

Then use something like this in the input:

<input type="text" onfocus="showFocusAlert()" />

Upvotes: 2

Related Questions