Jump to content
Sign in to follow this  
Pravin Kumar Raja

Javascript alert box repeating.

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;
			}

 

Share this post


Link to post
Share on other sites

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.

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...