Paul
Paul

Reputation: 630

Getting the value of an input element in jquery

I would like to get the value of a input using a the a chain class as the identifier in jquery. is this possible?

I have the following code

<input class="donate-block__value monthly" type="text" id="donation-amount" name="DonationAmount" value="200" />

and i have tried the following which has resulted in undefined

var monthlyDonation = $('.donate-block__value .monthly').val();
var monthlyDonation = $('donate-block__value monthly').val();

console.log(monthlyDonation);

I need to target the class Can this be done please?

Upvotes: 0

Views: 53

Answers (3)

larz
larz

Reputation: 5766

Your selector .donate-block__value .monthly is referring to an element within an element. Try just .donate-block__value or .monthly

Upvotes: 0

dchayka
dchayka

Reputation: 1302

You can also target just the input element in case you have multiple elements with that class defined:

$('input.donate-block__value.monthly').val();

Upvotes: 2

Cameron637
Cameron637

Reputation: 1719

Don't add space between your classes or jquery will start to search within the first class looking for a child element. use it like this:

var monthlyDonation = $('.donate-block__value.monthly').val();

Upvotes: 5

Related Questions