6

I need to focus out from the textbox when it focus in.

I try to set focus for outer div and its working fine in IE but not in mozilla.

How do I do this?

This is my current code:

<div id="outer"> <input type = "textbox" /></div> Onfocus: document.getElementById("outer").focus() 
4
  • Any code from your side? Commented Feb 3, 2010 at 12:19
  • <div id="outer"> <input type = "textbox" /></div> Onfoucs: document.getElementById("outer").focus() Commented Feb 3, 2010 at 12:23
  • Please put this into your question. Commented Feb 3, 2010 at 12:39
  • all the answer are not focus out from the text box Commented Feb 3, 2010 at 14:07

5 Answers 5

6

I wonder what's the purpose of using a textbox in this case if the user can never write anything inside. Just add a disabled="disabled" attribute or readonly="readonly" (in case you want to post the value).

Sign up to request clarification or add additional context in comments.

Comments

6

In HTML:

<input type="text" onfocus="this.blur();" /> 

In JS:

document.getElementById("input1").onfocus = function () { this.blur(); } 

Some elements cannot accept focus without being editable.

Comments

3

I have tried all the answers and not worked in all the browsers. And I combined all together in to it.

  1. TextBox.readonly = true;

OnFocus:

  1. var curText = TextBox.value; TextBox.value = ""; TextBox.value = curText;

  2. TextBox.blur();

  3. TextBox_Parent.focus()

And its working fine in all the browsers

Comments

2

Where is the point in that? JS would be (didn't test it):

$('#textbox').focusin(function() { $(this).focusout(); }); 

Comments

-2
/*for textarea*/ $(document).ready(function() { $('textarea[type="text"]').addClass("idleField"); $('textarea[type="text"]').focus(function() { $(this).removeClass("idleField").addClass("focusField"); if (this.value == this.defaultValue){ this.value = ''; } if(this.value != this.defaultValue){ this.select(); } }); $('textarea[type="text"]').blur(function() { $(this).removeClass("focusField").addClass("idleField"); if ($.trim(this.value == '')){ this.value = (this.defaultValue ? this.defaultValue : ''); } }); 

});

that's what I used on my form.

1 Comment

This code includes JQuery. The question is about vanilla javascript.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.