0

I want to set input type value blank which i just passon name attribute. for example

1) Multiline:

$('input[name="library"]').val('');
$('input[name="books"]').val('');
$('input[name="author"]').val('');

2) Single Line:

$('input[name="library"],input[name="books"],input[name="author"]').val('');

above both line is working for me. But I want more optimize way. Is there any way to write it like as follow

$('input[name="library"][name="books"][name="author"]').val('');

and it will blank all 3 input fields blank?

One more thing, I am getting these all input fields from other server, and I am not able to change input fields class on their server. So i just want to replace some of input fields on my side that i explain above.

4
  • show your markup.. so that we could think in different perspectives. Commented Aug 21, 2014 at 11:12
  • 1
    Something like $(':input').filter('[name="library"][name="books"][name="author"]').val('');? Commented Aug 21, 2014 at 11:19
  • @chridam Is that an optimized way.? Commented Aug 21, 2014 at 11:19
  • From the jQuery API documentation, :input is an extension that is not part of the CSS specification, queries using :input cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method but the best performance is achieved by first selecting the elements using a pure CSS selector, then using .filter(":input"). Commented Aug 21, 2014 at 11:23

6 Answers 6

1

You could use an array and function:

function inputEmpty( elem ) {
    $('input[name="'+elem+'"]').val('');
}

var names = [ "library", "books", "author" ];
names.forEach( inputEmpty );

Check the fiddle - http://jsfiddle.net/nLm6u75z/

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

Comments

0

Try this

$("input[type=text]").each(function() {
    $(this).val('My new value');
});

Comments

0

You can optimize the syntax a bit by removing 'input'; the [attr=val] syntax works by itself:

$('[name="library"],[name="books"],[name="author"]').val('');

Or are you looking for performance optimizations?

Comments

0

You can group all your inputs with the same className like : "inputGroup".

The answer of your problem will be :

$('.inputGroup').val('');

Comments

0

If you are looking for or-like syntax in the jquery selectors then the answer is no, there is no or operator in the selectors.

Your last approach is the most concise way to select what you want without loosing any preciseness.

$('input[name="library"][name="books"][name="author"]').val('');

Comments

-1

Surely the easiest way to do this is to add a class to all three inputs then change their value based on that class as a selector?

<input type="text" name="library" class="input_change" />


$('.input_change').val('')

2 Comments

thanks, this one i know, but i am getting these all input fields from other server, and I am not able to change input fields class on their server. So i just want to replace some of input fields on my side that i explain
Ah okay. Edit your question to reflect this so other people don't get confused. :)

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.