2

How to get all values of input type text with a specific class?

I tried this code. But it's not showing the expected answer.

$('input .classname').each(function(){
    console.log($(this).val());
});

3 Answers 3

8

This should select all input fields of type text with the class "classname".

$('input[type="text"].classname').each(function () {
    console.log($(this).val());
});
Sign up to request clarification or add additional context in comments.

Comments

3

One way is to assign a class for all your input elements, that way it will not interfere with unwanted input elements.

For example when you don't need to get all the input elements in the DOM

$('.test').each(function(){
    console.log($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class='test' value='wanted data'/>
<input value='unwanted data'/>

1 Comment

Yes! sometimes a gallery component creates unwanted duplicates of div's, had to use class names like this to ignore the duplicate hidden fields..
0

i think keyup is what you are looking for

$('.classname').keyup(function() {
  console.log($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<input type='text' class='classname'>

Comments

Your Answer

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