Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery clearing an input text field

Tags:

jquery

syntax

I have a question regarding clearing an input text field when a different drop down on teh same page is clicked, the value in the input text field should be cleared. Here is my jQuery ftn. The alert message is shown everytime I select an option from drop down but the field does not get cleared. Any idea what the correct syntax should be:

jQuery(document).ready(function(){

        jQuery("[id$='serialNumForm:noSerialNumProductKey']").change(function () {
            alert("In jquery change ftn!!");
            jQuery("[id$='serialNumForm:inputSN']").value="";
        });     
    });

I have a form Id so I need to prepend the Id to the element id name. the alert displays fine just the value does not change.

like image 464
msharma Avatar asked Dec 08 '09 18:12

msharma


People also ask

How do you clear the input field in text?

To clear an input field after submitting:When the button is clicked, set the input field's value to an empty string. Setting the field's value to an empty string resets the input.

How remove textbox value after submit in jQuery?

find('input:text'). val(''); $('input:checkbox'). removeAttr('checked'); }); One will clear all text inputs.

How do you empty a textbox in JavaScript?

You can use the onfocus attribute in JavaScript to clear a textbox or an input box when somebody sets focus on the field. If you are using jQuery, then use the . focus() method to clear the field.


2 Answers

You want to use:

jQuery("[id$='serialNumForm:inputSN']").val("");
like image 66
Justin Swartsel Avatar answered Oct 01 '22 06:10

Justin Swartsel


Calling "jQuery("[id$='serialNumForm:inputSN']")" doesn't return you an input element - instead it returns you a jQuery object. You have two options:

Use this to get the core element:

jQuery("[id$='serialNumForm:inputSN']").get(0)

Or, as Justin mentioned, use the jQuery val() method:

jQuery("[id$='serialNumForm:inputSN']").val("");

The second way is more reliable, since it applies the value to all matching elements, while the first only applies it to the first matching element (as indicated by the 0 in the get(0) )

like image 43
Mike Robinson Avatar answered Oct 01 '22 06:10

Mike Robinson