Kind of new to learning jQuery, I wrote this script.
<script type="text/javascript">
$(document).ready(function(){
alert($("#myInput").value);
alert($("#myInput").length);
});
</script>
<input id="myInput" value="Hello, World!" type="text" />
I don’t know why it is not why it always shows me:
undefined
1
While the expected outptu is:
Hello, World!
13
Please advice me. Thanks in advance.
These kind of questions have been discussed and answered many times. Always, do a search before answering. Let me give you some suggestions.
You have built-in functions like
.val(), which is mainly used for getting the current value of the form inputs, when you have different types of them like textbox, checkbox, radio, textarea, and each of them use different types. So, change your first script this way:The object returned by
$("#myInput")would be a jQuery collection of elements. So, there is only one<input type="text" id="myInput" />. You need to use$("#myInput").val().length, and that gives the length of the value in the textbox. So, change your second script to: