0

i need to alert entered textbox value so i am doing it as follow. Html code:

<input type="text" id="textbox1"  class="cal" />

JQuery code:

$(document).ready(function () {
    var get = $("#textbox").val();
   $("#calculate").click(function () {
     alert(get);

    });
}); 

At click,it always says undefined.Why? Thanks.

2 Answers 2

4

Change

var get = $("#textbox").val();

To

var get = $("#textbox1").val();

So the id you are targetting matches your HTML, and put it inside your event handler:

$(document).ready(function() {
   $("#calculate").click(function () {
       var get = $("#textbox1").val();
       alert(get);
    });
}); 

So you are getting the value when you click the button rather than on document load.

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

Comments

1

Your id is textbox1, and you should get the value when clicked, or you just get the initial value.

$(document).ready(function () {
   $("#calculate").click(function () {
       alert( $("#textbox1").val());
    });
}); 

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.