Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Setting Value of Input Field

Tags:

I have an input field and i am trying to set its value using its class

<form:input path="userName" id="userName" title="Choose A Unique UserName" readonly="${userNameStatus}" class="formData"/> 

Which renders as:

<input id="userName" name="userName" title="Choose A Unique UserName" class="formData" type="text" value=""/> 

I am using the following jquery but this does not seem to work. Can someone tell me where i am going wrong.

$(".formData").html(""); 

Edited JQuery Function that handles the event

$('#reset').click(function (){             $("#userNameErr").text("");              $('#badgeNoErr').text("");              $(".errors").html("");              $(".formData").val("");          }); 

nor is $(".formData").text("") or $(".formData").val("") working.

like image 433
devdar Avatar asked Oct 21 '12 03:10

devdar


People also ask

How can get input tag value in jQuery?

To get the textbox value, you can use the jQuery val() function. For example, $('input:textbox'). val() – Get textbox value.

How can add value in textbox using jQuery?

Answer: Use the jQuery val() Method You can simply use the jQuery val() method to set the value of an input text box.


Video Answer


2 Answers

This should work.

$(".formData").val("valuesgoeshere") 

For empty

$(".formData").val("") 

If this does not work, you should post a jsFiddle.

Demo:

$(function() {    $(".resetInput").on("click", function() {      $(".formData").val("");    });  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  <input type="text" class="formData" value="yoyoyo">      <button class="resetInput">Click Here to reset</button>
like image 87
Maktouch Avatar answered Nov 12 '22 16:11

Maktouch


If the input field has a class name formData use this : $(".formData").val("data")

If the input field has an id attribute name formData use this : $("#formData").val("data")

If the input name is given use this : $("input[name='formData']").val("data")

You can also mention the type. Then it will refer to all the inputs of that type and the given class name: $("input[type='text'].formData").val("data")

like image 29
MItrajyoti Kusari Avatar answered Nov 12 '22 17:11

MItrajyoti Kusari