1

var footerEmail = $('footer#footer input.email');
        var footerEmailLength = footerEmail.val().length;
        var footerEmailCaptcha = $("footer#footer .captcha-hide");

        footerEmail.focus( function() {
            footerEmailCaptcha.css("display","block");
        });


        footerEmail.blur( function() {
            if(footerEmailLength > 0) {
                footerEmailCaptcha.css("display","block");
            }
            else if (footerEmailLength == 0) {
                footerEmailCaptcha.css("display","none");
            }
        });
.captcha-hide {
  display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<footer id="footer">
  <input class='email'>
  <div class="captcha-hide">Testing</div>
</footer>

I want to make sure that onblur works when I enter something inside the text (input) field.

First if condition inside the blur function is not working since it is taking the value as '0' which will be initially. When I enter something and click outside of the input field then the css should be display:block

Please guide me how I can proceed further. I am new to jQuery/Javascript. Googling around to learn stuff.

4
  • Its kinda hard for us to help you specific without seeing all relevant code, HTML CSS Commented Apr 25, 2018 at 9:09
  • Your selectors are ridiculously long. As id should be unique within the DOM you can shorten it to #footer-subscriber-form .form-item-mail input.email at most, probably even less than that. Caching the object would help too Commented Apr 25, 2018 at 9:10
  • You're already creating a footerEmail variable. Why not use it in subsequent code instead of repeating that overly long selector? Commented Apr 25, 2018 at 9:11
  • I will edit and come back in few minutes. Commented Apr 25, 2018 at 9:17

1 Answer 1

1

you have to give the var footerEmailLength = footerEmail.val().length; inside blur function.

The blur function should be like this:

footerEmail.blur( function() {
        var footerEmailLength = footerEmail.val().length;
        if(footerEmailLength > 0) {
            footerEmailCaptcha.css("display","block");
        }
        else if (footerEmailLength == 0) {
            footerEmailCaptcha.css("display","none");
        }

And if you use class as selector then change the footerEmail.val().length to footerEmail[0].val().length.

The running code

var footerEmail = $('.email');
        
        var footerEmailCaptcha = $(".captcha-hide");


footerEmail.focus( function() {
            footerEmailCaptcha.css("display","block");
        });


        footerEmail.blur( function() {
            var footerEmailLength = footerEmail[0].val().length;
            if(footerEmailLength > 0) {
                footerEmailCaptcha.css("display","block");
            }
            else if (footerEmailLength == 0) {
                footerEmailCaptcha.css("display","none");
            }
        });
.captcha-hide {
  display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<footer id="footer">
  <input class='email'>
  <div class="captcha-hide">Testing</div>
</footer>

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

1 Comment

Thank you. It worked like a charm. It made sense to me now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.