How to select specific form element in jQuery?

jquery, jquery-selectors

Solution

It isn't valid to have the same ID twice, that's why `#name` only finds the first one.

You can try:

$("#form2 input").val('Hello World!');

Or,

$("#form2 input[name=name]").val('Hello World!');

If you're stuck with an invalid page and want to select all `#name`s, you can use the attribute selector on the id:

$("input[id=name]").val('Hello World!');

Problem

I have two form like this: ``` <form id='form1' name='form1'> <input name='name' id='name'> <input name='name2' id='name2'> </form> <form id='form2' name='form2'> <input name='name' id='name'> <input name='name2' id='name2'> </form> ``` Now I want to insert text in name field of form2. I am using following jQuery code but it fill name field of form1. ``` $("#name").val('Hello World!'); ``` So how to select specific form elements only?

Original source