views:

76

answers:

2

Hi guys,

I have a label, costLabel.

What I want to be able to do is change the value of this label depending on the selected value of a dropdownlist.

This is my HTML:

<table>
  <tr>
    <td width="343">Package*</td>

    <td colspan="4">
      <select class="purple" name="package">
        <option value="standard">Standard - &euro;55 Monthly</option>
        <option value="standardAnn">Standard - &euro;49 Monthly</option>            
        <option value="premium">Premium - &euro;99 Monthly</option>
        <option value="premiumAnn" selected="selected">Premium - &euro;89 Monthly</option>            
        <option value="platinum">Platinum - &euro;149 Monthly</option>
        <option value="platinumAnn">Platinum - &euro;134 Monthly</option>            
      </select>
    </td>

  <tr>
    <td width="343">
      <p>We bills quarterly/annually in advance</p>
      <p>See <a href="#dialog" name="modal">Pricing</a> for more details</p>
    </td>
    <td colspan="4"><label id="costlabel" name="costlabel">Total Cost:</label></td>

  <tr>
</table>

The values that go into the cost label are

  • Standard = "€165 Quarterly"
  • StandardAnn = "€588 Annually"
  • Premium = "€297 Quarterly"
  • PremiumAnn = "€1068 Annually"
  • Platinum = "€447 Quarterly"
  • PlatinumAnn = "€1608 Annually"

I did have the following code in place which calculated the cost depending on the dropdown menu, but the registration form has since changed to be more simpler(i.e. discountselection is not gone), and I'm having a bit of trouble adapting the jQuery. Can someone help?

$(function() {
  $("#discountselection, select[name=package], input[name=discount]").
    change(function() {
      var
        selected_value = $("#discountselection").val(),
        discount = [12, 24, 36],
        package = $("select[name=package]").val(),
        package_prices = {'standard': 49, 'premium': 85, 'platinum': 134 },
        cost = package_prices[package] * discount[selected_value-1];

      $("#costlabel").val(cost);
    });
});
A: 

val() is more like a shortcut for attr('value'). For your usage use text() or html() instead

sod
+2  A: 

I seem to have a blind spot as regards your html structure, but I think that this is what you're looking for. It should find the currently-selected option from the select input, assign its text to the newVal variable and then apply that variable to the value attribute of the #costLabel label:

jQuery

$(document).ready(
  function() {
    $('select[name=package]').change(
      function(){
        var newText = $('option:selected',this).text();
        $('#costLabel').text('Total price: ' + newText);
      }
      );
  }
  );

html:

  <form name="thisForm" id="thisForm" action="#" method="post">
  <fieldset>
    <select name="package" id="package">
        <option value="standard">Standard - &euro;55 Monthly</option>
        <option value="standardAnn">Standard - &euro;49 Monthly</option>            
        <option value="premium">Premium - &euro;99 Monthly</option>
        <option value="premiumAnn" selected="selected">Premium - &euro;89 Monthly</option>            
        <option value="platinum">Platinum - &euro;149 Monthly</option>
        <option value="platinumAnn">Platinum - &euro;134 Monthly</option>   
    </select>
  </fieldset>

    <fieldset>
      <label id="costLabel" name="costLabel">Total price: </label>
    </fieldset>
  </form>

Working demo of the above at: JS Bin

David Thomas
Hi David, thanks for the reply. That works perfect. What I wanted to do though, is assign a text value to the option in the dropdown box (see my post)...e.g.Standard = "€165 Quarterly"...StandardAnn = "€588 Annually"..Is there any way to do this?
TaraWalsh
@TaraWalsh; your question, when you asked it, was: "What I want to be able to do is change the value of this label depending on the selected value of a dropdownlist." Which the above code does. Has your question changed? If it has then could you clarify for me exactly what you're asking in your question? And as soon as I understand it I'll do my best to help further =)
David Thomas
@TaraWalsh, so...the code as posted displays (for option one) the message 'Standard - €55 Monthly' in the label. Are you just wanting to remove the 'Standard - ' part (and display **just** the '€55 Monthly' part)?
David Thomas
@David, I will mark this as the correct answer as I have not been clear. Thanks for your help.
TaraWalsh
@TaraWalsh; thanks, but if you're able to add an exact step-by-step description of your UI (what the user does, how your page responds), I'd rather solve the problem =) it might just be that I've had a long-ish day, though...
David Thomas
Sorry David, I've just posted it as a different question, under pressure to get it done and a complete newbie to jQuery. Here is the link though if you still want to give it a go:http://stackoverflow.com/questions/3871232/assign-value-to-dropdownlist-items-display-in-label
TaraWalsh
@TaraWalsh, I'm thinking my way through it as we speak, thanks for posting it separately, for some reason I think I understand it now =)
David Thomas