JQuery gets form field value

I am using a jquery template to dynamically create multiple elements on the same page. Each item is as follows:

<div id ="DynamicValueAssignedHere"> <div class="something">Hello world</div> <div class="formdiv"> <form name="inpForm"> <input type="text" name="FirstName" /> <input type="submit" value="Submit" /> </form> </div> </div> 

I would like to use jQuery to process the form on submit. I would also like to return the form values ​​to their previous values ​​if something goes wrong. My question is: How can I get the value of an input field using jQuery? For example, I can get the div value with the class "something" by doing

 var something = $(#DynamicValueAssignedHere).children(".something").html(); 

Similarly, I want to get the value of a text field. Right now i tried

 var text = $(#DynamicValueAssignedHere).children(".formdiv").findnext('input[name="FirstName"]').val(); 

but it doesn't seem to work

+65
javascript jquery
Jul 25 '12 at 16:32
source share
10 answers

You must use the value attribute to get its value

 <input type="text" name="FirstName" value="First Name" /> 

try -

 var text = $('#DynamicValueAssignedHere').find('input[name="FirstName"]').val(); 
+121
Jul 25 2018-12-12T00:
source share
β€” -

It can be a lot easier than what you do.

HTML:

 <input id="myField" type="text" name="email"/> 

JavaScript:

 // getting the value var email = $("#myField").val(); // setting the value $("#myField").val( "new value here" ); 
+45
Jul 25 2018-12-12T00:
source share

Alternative approach without html field search:

 var $form = $('#' + DynamicValueAssignedHere).find('form'); var formData = $form.serializeArray(); var myFieldName = 'FirstName'; var myFieldFilter = function (field) { return field.name == myFieldName; } var value = formData.filter(myFieldFilter)[0].value; 
+5
Nov 16 '15 at
source share

if you know the id of the inputs you only need to use:

 var value = $("#inputID").val(); 
+2
Jul 25 2018-12-12T00:
source share
 var textValue = $("input[type=text]").val() 

this will get all the values ​​of all text fields. You can use methods such as children, firstchild, etc., to push away. $ ('form [name = form1] input [type = text]') It’s easier to use identifiers for targeting elements, but if they are purely dynamic, you can get all the input values ​​and then loop through JS.

+2
Jul 25 '12 at 16:40
source share

 $("form").submit(function(event) { var firstfield_value = event.currentTarget[0].value; var secondfield_value = event.currentTarget[1].value; alert(firstfield_value); alert(secondfield_value); event.preventDefault(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="" method="post" > <input type="text" name="field1" value="value1"> <input type="text" name="field2" value="value2"> </form> 
+1
Aug 12 '16 at 11:45
source share
 // for getting the value from input type text var value = $("input[type=text]").val(); // for getting the value from by input type name var textValue = $("input[name=text]").val() 
+1
Jan 04 '17 at 12:06 on
source share

How to get an approved Upwork profile (even if you have been rejected 10 times).

How to get approval for your Upwork profile, even if they say your skill is not needed.

Email me to get approval for your #upwork account !!!!

skype: kumarworkonupwork@gmail.com [John Snow]

0
Jan 25 '19 at 6:31
source share

You can try these lines:

 $("#DynamicValueAssignedHere .formdiv form").contents().find("input[name='FirstName']").prevObject[1].value 
0
Jan 25 '19 at 7:22
source share

You can get any input field value with $('input[fieldAttribute=value]').val()

here is an example

 displayValue = () => { // you can get the value by name attribute like this console.log('value of firstname : ' + $('input[name=firstName]').val()); // if there is the id as lastname console.log('value of lastname by id : ' + $('#lastName').val()); // get value of carType from placeholder console.log('value of carType from placeholder ' + $('input[placeholder=carType]').val()); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="formdiv"> <form name="inpForm"> <input type="text" name="firstName" placeholder='first name'/> <input type="text" name="lastName" id='lastName' placeholder='last name'/> <input type="text" placeholder="carType" /> <input type="button" value="display value" onclick='displayValue()'/> </form> </div> 
0
Jan 31 '19 at 9:14
source share



All Articles