2

How can I set the value of a textBox value using Javascript?

I can read the value of the txtUserName field in my AJAX method. But in the two commented rows give an error:

  function handle_geolocation_query(position) {

       // $("input#MainContent_txtEnlem").val() = position.coords.latitude;
       // $("input#MainContent_txtBoylam").val() = position.coords.longitude;
        PageMethods.SendLocation($("input#MainContent_txtUserName").val(), position.coords.latitude, position.coords.longitude);
    }

I'm using jQuery 1.4.1.

3 Answers 3

4

The error in you code is that you are assigning the value to function. What you should do is pass the value as agrument, so your code will be:

$("#MainContent_txtEnlem").val( position.coords.latitude);
$("#MainContent_txtBoylam").val(position.coords.longitude);

Some other points:

  • If you are making use of id then there is no need to write input#....
  • This is a jQuery function. It's not pure JavaScript.
Sign up to request clarification or add additional context in comments.

1 Comment

Just to elaborate: val() returns the value. So you're trying to assigning the new value to the string that was returned, not to any property of the <input>.
0

You should be able to just use $("#textboxId").val("some text"); What error does it give when you try this?

Comments

0

I think it gives you an error because you're trying to assign a value to a function.

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.