user7106955
user7106955

Reputation:

jQuery - Get input value with specific class name

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());
});

Upvotes: 2

Views: 41466

Answers (3)

PLT
PLT

Reputation: 491

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

$('input[type="text"].classname').each(function () {
    console.log($(this).val());
});

Upvotes: 8

sa77
sa77

Reputation: 3603

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'>

Upvotes: 0

Hudhaifa Yoosuf
Hudhaifa Yoosuf

Reputation: 919

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'/>

Upvotes: 3

Related Questions