Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery set textbox collection to empty value

How do i go about emptying the values of textboxes here is below code i h've worked out but doesn't seem to work

var txtName = $("input#txtName"),txtPrice = $("input#txtPrice");

First Method

$(txtName,txtPrice).val("");
  • this is actually wrong because the price textbox would now become the context to search within i suppose.

Second Method

$([txtName,txtPrice]).val("");
  • I don't understand why i should do this as they are already jQuery Objects(But works)

I Put them in variables as these are used further in the script.

like image 881
Deeptechtons Avatar asked Dec 09 '22 03:12

Deeptechtons


1 Answers

Here is a few ways to do it;

txtName.add(txtPrice).val("");
// OR
$("input#txtName,input#txtPrice").val("");

(There is a $ sign in your txtPrice input by the way.)

First Method didn't work because it's a way of using jQuery selector. When you use jQuery like that first parameter will be the selector and second will be the container object where the selector works. Basically it's almost same thing like this;

$(txtPrice).find(txtName).val("");

Because there is no txtName in txtPrice neither value will be emptied.

Second Method works because you're giving your parameters as an array to jQuery selector. It accepts it and does .val() action to every element of array. This way is legit but because your variables already jQuery objects there is no need to use this.

like image 178
Emre Erkan Avatar answered Jan 03 '23 06:01

Emre Erkan