how to un-focus an element

  • Thread starter Burak Kadirbeyoglu
  • Start date
B

Burak Kadirbeyoglu

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));
 
J

John Timney \( MVP \)

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
 
B

Burak Kadirbeyoglu

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top