0

Why is the hidden form not shown when it looses focus? The alert is coming up nicely when leaving the input but the other hidden form is still not there.

html

<body>
   <input type="text" id="myinput" value="">
   <input type="hidden" id="validation_message_email" value="enter a valid email">
</body>

javascript

window.onload = function() {
   $("#myinput").blur(myAlert);
};

function myAlert() {
   alert("This input field has lost its focus.");
   $("#validation_message_email").show();
}
3
  • Its hidden element notice type="hidden" I think you need <span style="display:none" id="validation_message_email">enter a valid emai</span> Commented Jun 1, 2016 at 12:44
  • why have you taken "validation_message_email" hidden? Commented Jun 1, 2016 at 12:45
  • type='hidden' doesn't directly implies that the element is hidden, it means the control itself is a type known as 'hidden'. You cannot change it, so its recommended you use a <div> or <span> tag, set the css as display:none initially, and then later call the jquery show() function on the element. Commented Jun 1, 2016 at 12:47

6 Answers 6

5

You can't display a hidden input like that.A span will suit better for this purpose,

<input type="text" id="myinput" value="">
<span style="display:none"  id="validation_message_email">enter a valid email</span>
Sign up to request clarification or add additional context in comments.

3 Comments

Indeed. An <input> should be used for getting input from a user, not for displaying a message.
You should start with style="display:none" or else there is no need of .show()
@Satpal thanks for the suggetion. Just edited the answer.
1

validation_message_email doesn't have its display style property as none, so show() will not make it visible from type="hidden".

You need to replace

$("#validation_message_email").show();

with

$("#validation_message_email").attr( "type", "text" );

However, if the intent is to only show a message, then you don't need to use a hidden input for the same.

<body>
   <input type="text" id="myinput" value="">
</body>

and

window.onload = function() {
   $("#myinput").blur(function(){
      alert("This input field has lost its focus.");
      $(this).append('<span id="emailValidationMessage">enter a valid email</span>')
   });
   $("#myinput").focus(function(){
      $("#emailValidationMessage").remove();
   });
};

Comments

0

No need to use type="hidden" as hidden elements are not display:none they are hidden by default.

Use type="text" and hide it with css and show where you want

<input type="text" id="myinput" value="" style="display:none;">

Comments

0

use like this

<input type="text" id="myinput" value="">
<input type="hidden" id="validation_message_email" value="enter a valid email">

<script>   
window.onload = function() {
    $("#myinput").blur(myAlert);
};

function myAlert() {
   $("#validation_message_email").attr("type","text");
}
</script>

1 Comment

While this code block may answer the question, it would be best if you could provide a little explanation for why it does so.
0
 <div class="form-group" id="usernamediv">

 <input class="form-control" name="username" id="username"
     placeholder="Username" type="text"  required=""> </div>
 <div class="form-group" id="passworddiv">
  <input name="password" class="form-control" id="password" placeholder="Password"  type="password" required="">
  </div>
<button id="#loginButton">Login</button>
<button id="#forgotpassword">Forgot Password</button>


<script>
$("#forgotpassword").hide();
$("#forgotpassword").click(function(e){
$("#loginButton").hide();
$("#usernamediv").show();
$("#passworddiv").hide();
})


</script>

1 Comment

While this code block may answer the question, it would be best if you could provide a little explanation for why it does so.
0

Check this jsfiddle link, it might help you.

$("#myinput").blur( function(){

myAlert();

});

function myAlert() {
   $("#validation_message_email").attr("type", "text");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<input type="text" id="myinput" value="">
<input type="hidden" id="validation_message_email" value="enter a valid email">

1 Comment

While this code block may answer the question, it would be best if you could provide a little explanation for why it does so.

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.