paddle42380
paddle42380

Reputation: 7081

Retrieving the Value of an input text field in Jquery

Absolute noob question.

I have a form

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

When the user clicks on a link , my jquery function needs to pick up the current value of this input field.

How do I retrieve this value? Doing a $("#cars").val gives me a block of html and not the string typed in by the user, Also $("cars").text throws a null value.

Any suggestions?

Upvotes: 0

Views: 1118

Answers (2)

Jos
Jos

Reputation: 325

var carValue = $("#cars").val();

carValue will give you current value.

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630637

Make sure you're calling it as a function (with parenthesis!), $("#cars").val() will give you want you want here.

For example:

$("button").click(function() {
    alert($("#cars").val());
});​

You can test it out here, without the parenthesis you're getting the function, not executing it and getting the result, see what I mean in a demo of that here.

Upvotes: 4

Related Questions