Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keep input element focused after right click.

Status
Not open for further replies.

sharapov

MIS
May 28, 2002
106
US
I am designing an Intranet application. In my application I need the focus always be on input text field. I also need to disable the context menu. I am able to achieve all that with the code below. However, when I right click anywhere on the page I loose focus on my input field. The only way I can bring it back if I click anywhere on the page with the left button. How can I keep focus on my input element when I right-click the page?



<html>
<head>
<title></title>
</head>

<body onclick=&quot;document.oForm.searchval.focus()&quot; onload=&quot;document.oForm.searchval.focus()&quot; oncontextmenu=&quot;return false&quot;>

<form name=&quot;oForm&quot;>
<input onblur=&quot;this.focus();&quot; type=&quot;text&quot; name=&quot;searchval&quot; size=&quot;20&quot;><br><br>
<input type=&quot;text&quot; name=&quot;T2&quot; size=&quot;20&quot;><br><br>

<button>Test</button>
</form>

</body>
</html>
 
not sure if it will work but try oncontextmenu=&quot;return false; document.oForm.earchval.focus()&quot;>
 
swap the return and the focus maybe?
Code:
..=&quot;document.oForm.searchval.focus();return false&quot;

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
That way it won't work either. I found a different solution, however. Check it out:

<html>
<head>
<script language=&quot;javascript&quot;>
document.oncontextmenu=new Function(&quot;document.oForm.searchval.focus();return false&quot;);
</script>
<title></title>
</head>
<body onload=&quot;document.oForm.searchval.focus()&quot; onclick=&quot;document.oForm.searchval.focus()&quot;>
<form name=&quot;oForm&quot;>
<input type=&quot;text&quot; name=&quot;searchval&quot; size=&quot;20&quot;><br><br>
<input type=&quot;text&quot; name=&quot;T2&quot; size=&quot;20&quot;><br><br>

<button>Test</button>
</form>

</body>
</html>
 
What happens when they turn off javascript?

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
It's for Intranet, so I can cantrol weather they can turn off javascript or not.

By the way, I found a bug in the solution that I proposed (above). If you double right click the text field losses focus. Any suggestions to go around it?
 
Spazman,

I tried that. it doesn't work. I think doubleclick only works if you double click with the left mouse button not the right one. (I guess I should have mentioned that in thge previous post. If you double click with the right mouse button, text field losses focus)
 
I have found a solution to my problem. Here is the code for those interested:


<html>
<head>
<title></title>
</head>
<body onclick=&quot;document.oForm.searchval.focus()&quot;
onload=&quot;document.oForm.searchval.focus()&quot;
oncontextmenu=&quot;if(document.all)document.body.focus();return false&quot;>
<form name=&quot;oForm&quot;>
<input onblur=&quot;this.focus();&quot; type=&quot;text&quot; name=&quot;searchval&quot; size=&quot;20&quot;><br><br>
<input type=&quot;text&quot; name=&quot;T2&quot; size=&quot;20&quot;><br><br>
<button>Test</button>
</form>
</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top