Need Urgent Help Fast - Simple Javascript Problem (I Hope)

Discussion in 'JavaScript' started by nyxano, Nov 10, 2008.

  1. #1
    Hello,

    I'm not good at Javascript programming so I don't even know if this is possible.

    I have a price displayed on my web site with an option select box above it. So it would look like this:

    
    <select name="idOption1">
      <option value="1" priceincrement="0.00">Option 1</option>
      <option value="2" priceincrement="1.50">Option 2</option>
      <option value="3" priceincrement="3.00">Option 3</option>
    </select>
    
    <span id="listPrice">$2.95</span>
    
    Code (markup):
    What I would like to know is whether it is possible for Javascript to update the "listPrice" automatically when an option is selected?

    In other words, if the 3rd option is selected, the $2.95 changes to $5.95 automatically.

    I don't need to worry about quantities - just the price per item changing.

    Any help would be so greatly appreciated. Thanks! Sorry, I can't post a link because I don't have one for this site yet.
     
    nyxano, Nov 10, 2008 IP
  2. rene7705

    rene7705 Peon

    Messages:
    233
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    HTML:
    
    <select name="idOption1" onchange="priceChanges(this,event)">
      <option value="1" priceincrement="0.00">Option 1</option>
      <option value="2" priceincrement="1.50">Option 2</option>
      <option value="3" priceincrement="3.00">Option 3</option>
    </select>
    
    <span id="listPrice">$2.95</span>
    
    Code (markup):
    JS you'll have to include somewhere:

    
    function priceChanges (element, event) {
      var span = document.getElementById ('listPrice');
      var price = parseFloat(span.innerHTML);
      price = price + parseFloat(element.value);
      span.innerHTML = '$' + price;
    }
    
    Code (markup):
    I haven't tested this code, but think it has a chance of working like this..
     
    rene7705, Nov 11, 2008 IP