Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select particular input field name of particular form

i have some html code like this

<form name="first"><input name="firstText" type="text" value="General" /> 
<input name="secondText" type="text" value="General" /> 
<input name="ThirdText" type="text" value="General" /> 
<input name="FourthText" type="text" value="General" /> 
<input name="FifthText" type="text" value="General" />
</form>

<form name="second"><input name="firstText" type="text" value="General" /> 
<input name="secondText" type="text" value="General" /> 
<input name="ThirdText" type="text" value="General" /> 
<input name="FourthText" type="text" value="General" /> 
<input name="FifthText" type="text" value="General" />
</form>

i want to select "secondText" of form "second" using jquery or javascript and i want to change value of it using jquery.

like image 865
Nitz Avatar asked Nov 01 '13 13:11

Nitz


People also ask

Why name is used in input field?

The name attribute specifies the name of an <input> element. The name attribute is used to reference elements in a JavaScript, or to reference form data after a form is submitted. Note: Only form elements with a name attribute will have their values passed when submitting a form.


1 Answers

Using jQuery:

var element = $("form[name='second'] input[name='secondText']");

Using vanilla JS:

var element = document.querySelector("form[name='second'] input[name='secondText']");

Changing the value: element.val(value) or element.value = value, depending of what you are using.

like image 179
undefined Avatar answered Nov 11 '22 19:11

undefined