t56k
t56k

Reputation: 6981

jQuery: set value of text field from text label of select menu

I'm trying to set the value of a text field from the text (not the value) of a <select>, and I can't get it to work.

$('.prices_for_' + <%= d.id %>).change(function() {
  $('#order_quantity').text($(this).text())
  $('.price_display').text('$' + $(this).val() + '.00')
});

The second line works, the first doesn't. Any ideas?

Upvotes: 0

Views: 72

Answers (1)

dave
dave

Reputation: 64657

Going off the title where you said you want to set it from the text of the select menu - you could use an attribute selector to .find the option with the value that was selected, and then call .text() on it:

$('.prices_for_' + <%= d.id %>).change(function() {
    var text = $(this).find('option[value="' + $(this).val() + '"]').text();
    $('#order_quantity').text(text);
    $('.price_display').text('$' + $(this).val() + '.00')
});

Upvotes: 1

Related Questions