36

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.

12
  • What's the actual HTML that's produced? As opposed to whatever server-side script you're working with? Commented Oct 21, 2012 at 3:25
  • the actual html is <input id="userName" name="userName" title="Choose A Unique UserName" class="formData" type="text" value=""/> Commented Oct 21, 2012 at 3:27
  • I am trying to set the value in this field to null when i click on a reset button Commented Oct 21, 2012 at 3:27
  • Seems to be behaving fine: jsfiddle.net/FtnLM :) Commented Oct 21, 2012 at 3:28
  • "trying to set the value in this field to null" - To null? Or to an empty string? $(".formData").val("") should do it. Commented Oct 21, 2012 at 3:28

7 Answers 7

79

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>

Sign up to request clarification or add additional context in comments.

1 Comment

This does not work its a Spring tag i am using so i am not sure if its different but it renders normal html so i am not sure whats the problem
6

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

Comments

1

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

2 Comments

So why is it my code isnt working i have this in the document.ready??
u had ur initial setting to nowrap (head) . . .it works on domREADY ... i can't figure what is the problem with nowrap head setting
1

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

Comments

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

Comments

0

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

Comments

-5

Put your jQuery function in

$(document).ready(function(){

});

It's surely solved.

1 Comment

Please edit your other answer instead of posting a new answer. Also take a look at the formatting help: stackoverflow.com/editing-help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.