Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove focus from an input field in a jquery dialog

I have a button onclick of which a jquery dialog appears with two input fields which are jquery datepickers. Whenever the dialog appears, the first fields gets focus automatically, hence the datepicker pops up. I have resolved this for now by adding another dummy input field. Is there a better way ?

Sorry if the question is repeated. I tried searching for one already existing but couldn't get one. Please point me to an existing question if present.

Edit:

Okay my code goes as follows. onClick of a button the jdialog appears and there are 2 datepicker input fields inside the div "select_date_dialog".

$("#select_date").click(function(){     $("#select_date_dialog").dialog({         modal: true,         dialogClass: 'connect-dialog',         height: 100,         width: 500     }); }); 

afaik: can you be a bit more specific how blur will help me ? I tried adding this as suggested.

open: function(event, ui) {                 $('#custom_from_date').blur();             } 

blur happens when there input field loses focus. can i use blur to specify whether that field should get focus or not ?

like image 724
Alok Swain Avatar asked Nov 25 '10 11:11

Alok Swain


People also ask

How do you remove input field focus?

The blur() method removes focus from an element.

How to focus out in jQuery?

The focusout() method attaches a function to run when a focusout event occurs on the element, or any elements inside it. Unlike the blur() method, the focusout() method also triggers if any child elements lose focus. Tip: This method is often used together with the focusin() method.

How to set focus out in JavaScript?

To set focus to an HTML form element, the focus() method of JavaScript can be used. To do so, call this method on an object of the element that is to be focused, as shown in the example. Example 1: The focus() method is set to the input tag when user clicks on Focus button.

How do you know if input field is in focus?

To check if an input field has focus with JavaScript, we can use the document. activeElement property to get the element in focus. to add an input. to check if the input element is focused.


1 Answers

You could create a handler for the dialog's open event and blur the field in that:

$("#mydiv").dialog({    open: function(event, ui) {         $('#theinput').blur();     } }); 

I'd need to see more of your code to be any more specific though.

like image 119
Mark Bell Avatar answered Sep 21 '22 04:09

Mark Bell