all groups > dotnet jscript > december 2005 >
You're in the

dotnet jscript

group:

how to un-focus an element


how to un-focus an element Burak Kadirbeyoglu
12/7/2005 12:06:30 PM
dotnet jscript:
Dear Developers,

I have a simple question. There's a textbox and an imagebutton on my page.
When I focus on the textbox and hit enter, the imagebutton is being clicked.
However, when I click somewher else on the page and hit enter, the
imagebutton gets clicked again. How can I unfocus the imagebutton?
The script I'm implementing is given below.
Thanks in advance,

Burak

<script language="JavaScript" type="text/javascript">
<!--

function clickButton(strID) {
var pButton = document.getElementById(strID);
if (event && event.which) {
if (event.which == 13) {
pButton.click();
return false;
}


} else if (window.event && window.event.keyCode) {
if (event.keyCode == 13) {
event.returnValue = false;
event.cancel = true;
pButton.click();
return false;
}
}
}

function Buttonclick(strID) {
var textBox = document.getElementById(strID);
if(textBox.value == "")
{
alert('Please enter keyword(s)...');
return false;
}
}

// -->
</script>



private const string OnKeyPressFormatString = "return(clickButton('{0}'));";
private const string OnClickFormatString = "return(Buttonclick('{0}'));";


SearchTextBox.Attributes.Add("onKeyPress",
String.Format(OnKeyPressFormatString, buttonClientID));
SearchImageButton.Attributes.Add("onClick",String.Format(OnClickFormatString,textBoxID));
SearchButton.Attributes.Add("onClick",String.Format(OnClickFormatString,textBoxID));
Re: how to un-focus an element John Timney ( MVP )
12/7/2005 12:11:46 PM
Javscript has a focus event, used to move focus from element to element,
divs however can be tricker things to set focus against as focus normally
applies to form elements.

Try something like this:

document.getElementById(yourdivname).focus();

Regards

John Timney
ASP.NET MVP
Microsoft Regional Director



[quoted text, click to view]

Re: how to un-focus an element Burak Kadirbeyoglu
12/19/2005 11:33:03 AM
Well, even if I set the focus on another element, the imagebutton gets
clicked again when I anywhere on the page then press enter again. I want the
function Buttonclick() to run when the focus is on the textbox and the Enter
key pressed without entering any character. Any suggestions?

Burak

[quoted text, click to view]

Re: how to un-focus an element John Timney ( MVP )
12/19/2005 9:51:00 PM
browsers behave wierdly when enter is pressed, and asp.net doesn't always
recieve the submit - best thing to do is cancel the enter key out entirely.
Have a read of this thread.

http://groups.google.co.uk/group/microsoft.public.dotnet.framework.aspnet/browse_thread/thread/35c567ea942e5b18/7b8d4820ce9f0e90?lnk=st&q=asp.net+autosubmit&rnum=10&hl=en#7b8d4820ce9f0e90

--
Regards

John Timney
Microsoft MVP

[quoted text, click to view]

AddThis Social Bookmark Button