tags:

views:

31

answers:

2

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?

+4  A: 

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.

Nick Craver
Thanks, this worked! Also, is jsfiddle.net a new initiative?
papdel
@b_ayan - It's just a handy tool a few of the mootools put together, there are a few out there, but I personally prefer it to jsbin and such :)
Nick Craver
A: 

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

carValue will give you current value.

Jos