1

I have a form like this

<form action="">
  <label for="name">Name</label>
  <input type="text" id="name" />
</form>


<script type="text/javascript">
  jQuery(document).ready(function() {
    var Name = jQuery('#name').val();
    jQuery('#name').blur(function() {
      console.log(Name);
    });
  });
</script>

but here when I am entering any values in the input text field it is showing error like (an empty string). So can someone tell me what is the wrong here? How to solve this?

2
  • 4
    You didn't close the id of your input, and use no empty action tags. Commented Nov 28, 2013 at 11:59
  • Yes, for the closing of the ID, but empty action is fine. It just assumes the current url if it doesn't get passed one. Commented Nov 28, 2013 at 12:01

5 Answers 5

4
<input type="text" id="name />

should be

<input type="text" id="name" />

working fiddle: http://jsfiddle.net/TdLH2/

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

2 Comments

i think your answer is not correct, the OP may have had a typing mistake, please correct the answer, before someone down votes or flags
You are right, it had trouble accessing the Name variable outside the blur function.
3

This line:

var Name = jQuery('#name').val();

is a one time operation, setting the value of Name to the value of the input when that code executes. It will not magically update the value of Name when the value of that element changes (why do so many people get the impression that it would?). You'll need to do this:

jQuery(document).ready(function () {
    jQuery('#name').blur(function () {
        console.log(this.value);
    });
});

I've cut out the now pointless Name variable, and just used the element reference (this) directly to get its value property rather than using jQuery functions.

Comments

2

Name variable should be set inside onblur handler:

jQuery('#name').blur(function () {
    var Name = jQuery(this).val(); //or this.value
    console.log(Name);
});

Comments

0

Try

<form action="">
  <label for="name">Name</label>
  <input type="text" id="name" />
</form>


<script type="text/javascript">
  jQuery(document).ready(function()
   {
    jQuery('#name').blur(function () {
    var Name = jQuery("#name").val();
    jQuery("#name").blur(function() 
     {
      console.log(Name);
    });
   });
  });
</script>

Comments

0

If you want to console.log whatever is typed into the box, you can do:

<script>
  jQuery(document).ready(function() {
    jQuery('#name').keyup(function() {
        var Name = jQuery('#name').val();
        console.log(Name);
    });
  });
</script>

At the moment, you're getting the value of #name on page load, which would be "", an empty string. As nothing has been entered.

Comments

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.