John Smith
John Smith

Reputation: 6259

Get the value of first input in form

I have this HTML code:

<form accept-charset="UTF-8"  id="SearchPatient">
   <input autocomplete="off" class="form-control" placeholder="Patient suchen .." type="text">
</form>

And this JavaScript function:

$("#SearchPatient").submit(function () {
    Patient.all(1, this.find('input').value)
});

How you can guess i try o achieve that when the user submits the form the value of the input is passed to the function Patient.all(int,value)

Somehow i cant figure out how to get the first input in the form, i always get errors! For this code example i get the error:

Object # has no method 'find'

How can i do it correctly?

Upvotes: 0

Views: 1487

Answers (3)

Edward
Edward

Reputation: 3081

If you want the first input child of your form use

$(this).children("input[type='text']:first").val();

Upvotes: 1

Gianfranco Reppucci
Gianfranco Reppucci

Reputation: 304

You are missing a $( ).

The correct code should be:

$("#SearchPatient").submit(function(){Patient.all(1,$(this).find('input').value)});

Upvotes: 1

Rob Sedgwick
Rob Sedgwick

Reputation: 5226

Use

$(this).find('input').val();

We need to pass this in the jquery function to make use of the jquery methods

Update: as comment , to get the first-

$(this).find('input:first').val();

Upvotes: 3

Related Questions