Jump to content

Javascript alert box repeating.


Pravin Kumar Raja

Recommended Posts

I had 2 INPUT boxes and one BUTTON. I want to take the control back to the current INPUT after BUTTON is clicked. That part is working fine with the HTML file shown below:

<!DOCTYPE html>

<html>
	<head>
		<script>
			var last;
			function Focus(input) {
				last = input;
			}
			function Blur(input) {
			}
			function Click(button) {
				if (typeof last != "undefined")
					last.focus();
			}
		</script>
	</head>
	<body>
		<input id="I1" onfocus="Focus(this)" onblur="Blur(this)"> </input>
		<input id="I2" onfocus="Focus(this)" onblur="Blur(this)"> </input>
		<button id="B" onclick="Click(this)"> Click </button>
		<script>
		</script>
	</body>
</html>

But I wanted to code onblur function Blur to incorporate check whether the input is exited with a blank string and to return control to the blank INPUT field after displaying an alert to that effect. This displays the alert box continuously. Can you suggest how this can be avoided. Here is my code:

 

			function Focus(input) {
				if (typeof last != "undefined")
					if (last.value.length == 0)
						alert("Blank!");
				last = input;
			}

 

Link to comment
Share on other sites

  • 2 weeks later...

Well that would be because your alert code is inside your Focus function and not your Blur function.

So the alert is executing (after you've focused), then when you close the alert it refocuses which causes the alert again.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...