Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

JavaScript focus after a submit button

Featured Replies

hi,

 

I have a submit button in a web form. I want to write some javascript such that: after I submit the form, an alert window pop out, after I click ok, it focus on an input text field.

 

the problem is, after the submit button, the page refrsh and it won't focus on it any more.

 

could you please tell me how to solve this problem?

 

 

 

<html>

<head>

 

<script type="text/javascript">

function getfocus()

{

alert("hello")

document.getElementById('myAnchor').focus();

}

 

function losefocus()

{

document.getElementById('myAnchor').blur();

}

</script>

</head>

 

<body>

<a id="myAnchor2" href="http://www.w3schools.com">Visit W3Schools.com</a>

<input type="text" id="myAnchor" />

<br /><br/>

<form>

<input type="submit" onclick="getfocus()" value="Get focus">

<input type="button" onclick="losefocus()" value="Lose focus">

</form>

 

 

</body>

 

</html>

It looks like you might have to have your getfocus() function return false. This should tell the browser to not submit the form. You'll have to submit the form via javascript when you want it to submit though.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.