Avatar of flynny
flynny
Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

jquery textbox not focusing

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?
JavaScriptjQuery

Avatar of undefined
Last Comment
flynny

8/22/2022 - Mon