Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear all textboxs in a DIV using jQuery?

Tags:

jquery

asp.net

I have some asp textboxs in a div container. I just want to clear those when i am clicking the CLEAR button.

I say common class 'text' for all textboxes and then wrote this jQuery

$(".text").text(""); 

It's not working ..

How to do this ? I need the most efficient code .

like image 935
Vibin Jith Avatar asked Feb 27 '10 10:02

Vibin Jith


People also ask

How to clear the value in jQuery?

JavaScript reset(): The reset() method resets the values of all elements in a form (same as clicking the Reset button). The button itself resets the form but, the type-1 and type-2, the reset button should be inside the form and type-3 can be either out or in. Syntax to convert jQuery element to a JavaScript object.

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

If you need to clear text boxes values alone, you can change the code like this:

$("#divID").find("input[type=text]").val('');  

or

$("#divID").find("input:text").val('');
like image 95
vkGunasekaran Avatar answered Sep 27 '22 23:09

vkGunasekaran


$('a.clearButton').bind('click', function() {
    $('#divId input').val('');
});

Notes:

  1. You should use val() instead of text().

  2. You are asking for efficient code - and using class selector is not efficient.

    Either use id, or add tag name.

like image 33
Sagi Avatar answered Sep 27 '22 22:09

Sagi