I want 15% discount to be displayed automatically on the webpage once the user selects SE15 as the discount code(when the function is called)
this is the code i have, its not working, what could be the problem

<script type="text/javascript">
function getDiscount(){
var discount_code=document.orders.discount_code.options[document.orders.discount_code.selectedIndex].value;
var discount=document.orders.discount.options[document.orders.discount.selectedIndex].value;
var answer=document.orders.answer.value;

if(discount_code=="SE15")
discount=answer-(answer/100*15)
else
discount=0

document.orders.answer.value= "$ " + discount
}
</script>

You could try this approach:

function getDiscount(x, containerId){
  var menu = document.getElementById('discount_code');
  var container = document.getElementById('containerId');
  if(menu && container){
    var discount = Number(menu[menu.selectedIndex].value)/100;//option value must be "15", not "SE15".
    container.innerHTML = x * (1 - discount);
  }
}

Then control the detailed behaviour by supplying suitable arguments where getDiscount() is called.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.