devdar
devdar

Reputation: 5654

Jquery Setting Value of Input Field

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.

Upvotes: 36

Views: 200455

Answers (7)

Maktouch
Maktouch

Reputation: 3247

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>

Upvotes: 79

Stanislav Potapenko
Stanislav Potapenko

Reputation: 397

$('.formData').attr('value','YOUR_VALUE')

Upvotes: 1

MItrajyoti
MItrajyoti

Reputation: 578

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")

Upvotes: 6

sanjay sonule
sanjay sonule

Reputation: 21

use this code for set value in input tag by another id.

$(".formdata").val(document.getElementById("fsd").innerHTML);

or use this code for set value in input tag using classname="formdata"

$(".formdata").val("hello");

Upvotes: 1

sanjay sonule
sanjay sonule

Reputation: 21

Put your jQuery function in

$(document).ready(function(){

});

It's surely solved.

Upvotes: -5

sanjay sonule
sanjay sonule

Reputation: 21

You just write this script. use input element for this.

$("input").val("valuesgoeshere"); 

or by id="fsd" you write this code.

$("input").val(document.getElementById("fsd").innerHTML);

Upvotes: 0

palerdot
palerdot

Reputation: 7642

change your jquery loading setting to onload in jsfiddle . . .it works . . .

Upvotes: 1

Related Questions