Solved

jquery textbox not focusing

Posted on 2014-02-28
8
1,037 Views
Last Modified: 2014-03-05
Hi all,

Im hoping im missing something really simple here :).

I have the following code which is testing validation on textboxes; (please forgive the alerts)

<asp:TextBox ID="FirstNameTextbox" CssClass="half" runat="server" MaxLength="30"></asp:TextBox>

    <script type="text/javascript">
        $('#<%=FirstNameTextbox.ClientID%>').watermark('First name');

        $(document).ready(function () {
            $('#<%=FirstNameTextbox.ClientID%>').focusout(function () {

                if ($('#<%=FirstNameTextbox.ClientID%>').val() == "") {
                    alert('about to focus out');
                    highlightField($('#<%=FirstNameTextbox.ClientID%>'), false);
                    alert('finished!!!!');
                }
                else {
                    highlightField($('#<%=FirstNameTextbox.ClientID%>'), true);
                }

                alert('got here no issues');
            })

            $('#<%=FirstNameTextbox.ClientID%>').focusin(function () {
                if ($("#<%=FirstNameTextbox.ClientID%>").hasClass("error")) {
                    highlightFieldMessage($('#<%=FirstNameTextbox.ClientID%>'), false, "Please enter your first name in full.");
                    alert('here okkkk');
                }
             })
        });

        function highlightField(textfield, valid) {
            //alert('now here');

            if (textfield.parent().is(".textbox-wrapper")) {
               // alert( textfield.parent().html());
                textfield.parent().find(".input-status").remove();
                //alert('removed the status icon');
                //alert(textfield.parent.html());

                textfield.parent().find(".error-popup").remove();
                textfield.parent().find(".valid-popup").remove();
                //alert('removed any popups');
                textfield.unwrap();
                //alert('unwrapped');
            }

            //alert('adding the wrapper');
            textfield.wrap("<div class='textbox-wrapper'></div>");

            if (!valid) {
                textfield.removeClass("valid"); //just in case
                textfield.addClass("error");
                textfield.parent().append("<div class='input-status input-cross'></div>");
            }
            else {
               // alert('setting textbox invalid');
                textfield.removeClass("error"); //just in case
                textfield.addClass("valid");
                textfield.parent().append("<div class='input-status input-tick'></div>");
            }
        }

        function highlightFieldMessage(textfield, valid, message) {
           // alert('highlightField :' + textfield.attr('id') + ":valid:" + valid + ":MESSAGE:" + message );
            highlightField(textfield, valid);
            alert('here we are afgter sub call');

            if (!valid) {
                textfield.parent().append("<div class='error-popup'>" + message + "<div class='error-popup-arrow'></div></div>");
                textfield.parent().find('.error-popup').css('left', textfield.width() + 58);   
            }
            else {
                textfield.parent().append("<div class='valid-popup'>" + message + "<div class='error-popup-arrow'></div></div>");
                textfield.parent().find('.valid-popup').css('left', textfield.width() + 58);
            }
            alert('ok after adding message etc')
            
        }

Open in new window


Now when I first load and enter the field (i can enter text at this point into the field) and move out of focus from it it works correctly and highlights it as an error field.

When I move it back into focus it remains highlighted as error and displays the message. However at this point the field is not in focus?? i.e. i cannot type into it.

Am i knocking it out of focus in the code or doing something wrong here?
0
Comment
Question by:flynny
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39894651
you should not use alert because your field will lost focus
0
 

Author Comment

by:flynny
ID: 39894662
hi,

just to add it seems to be the sub call which unwraps and the rewraps the textbox

http://stackoverflow.com/questions/4350566/jquery-textfield-loses-focus-on-unwrap

unwrap seems to lose the focus of the textbox. Is there anyway I can stop this?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39895042
What about :
var f = textfield.is(":focus");
textfield.unwrap();
if(f) textfield.focus();

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:flynny
ID: 39895134
ok I've tried this but still the same result and it loses focus;

heres my updated code

function highlightField(textfield, valid) {

            var f = textfield.is(":focus");
            unwrap(textfield);

            textfield.wrap("<div class='textbox-wrapper'></div>");
            if (f) textfield.focus();

            if (!valid) {
                textfield.removeClass("valid"); //just in case
                textfield.addClass("error");
                textfield.parent().append("<div class='input-status input-cross'></div>");
            }
            else {
                textfield.removeClass("error"); //just in case
                textfield.addClass("valid");
                textfield.parent().append("<div class='input-status input-tick'></div>");
            }
        }

function unwrap(textfield) {
            if (textfield.parent().is(".textbox-wrapper")) {
                textfield.parent().find(".input-status").remove();

                textfield.parent().find(".error-popup").remove();
                textfield.parent().find(".valid-popup").remove();
                textfield.unwrap();

            }
        }

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39895149
Try this :
function highlightField(textfield, valid) {

            var f = textfield.is(":focus");
            unwrap(textfield);
            var myfield = textfield;
            textfield.wrap("<div class='textbox-wrapper'></div>");
            if (f)  setTimeout(function() { myfield.focus(); }, 1000); // reduce if it work

            if (!valid) {
                textfield.removeClass("valid"); //just in case
                textfield.addClass("error");
                textfield.parent().append("<div class='input-status input-cross'></div>");
            }
            else {
                textfield.removeClass("error"); //just in case
                textfield.addClass("valid");
                textfield.parent().append("<div class='input-status input-tick'></div>");
            }
        }

function unwrap(textfield) {
            if (textfield.parent().is(".textbox-wrapper")) {
                textfield.parent().find(".input-status").remove();

                textfield.parent().find(".error-popup").remove();
                textfield.parent().find(".valid-popup").remove();
                textfield.unwrap();

            }
        }

Open in new window

0
 

Author Comment

by:flynny
ID: 39895179
perfect thank you.

any ideas why .focus() was not working out of interest? i assume maybe wrap was still doing something?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39895195
there's no wizard in the room, to wrap, the element to wrap is removed from the DOM (so loose focus) the wrap element is added to the DOM, the element is added back to the DOM with all its saved attributes (alas not the focus which is not an attribute...)
0
 

Author Closing Comment

by:flynny
ID: 39905852
perfect
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question