0

I have the following code:

$(document).ready(function() {
    var phone_number = $('input[name=phone_number]').val();
    alert(phone_number);
});

The problem is that phone_number always have value on moment when a page was loaded. User may change phone number on the input fied - but script always gets obsolete value. How do I fix this issue?

Thanks a lot for the help!

1
  • You can fire an event and then write the lines of code you have written to do that I guess Commented Feb 12, 2013 at 10:23

4 Answers 4

2

Call this function on click of any button
Say you have a button

<input type="button" value="Get Value" id="getvalue" />

On its click get the value of the field

$(document).ready(function() { 
    $('#getvalue').click(function() {
         var phone_number = $('input[name=phone_number]').val();
         alert(phone_number);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

If you write this Get Value Script in document.ready... it always gives you the Default Value..

Try Get Value Script in any one the element click event...

Because : here document.ready = Page Load Event...

Try this Fiddle : http://jsfiddle.net/RYh7U/74/

Comments

1

As asifsid88 is right but if user may change phone number then after change phone number you want value of field so instead of click event , i think you should use change event.

Comments

1

Here you have written this script on document.ready function, that's why it returns obsolete value.
Put this script in some event i.e click, keypress,blur,onchange etc... So that you can get the changed value.

<input type="text" name="phone_number" id="phone_number" value="Get Value"/>

$(document).ready(function() { 
    $('#phone_number').blur(function() {
         var phone_number = $(this).val();
         alert(phone_number);
    });
});

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.