0

I have this WORKING code here:

function updatetotal (section, pricetoadd)         
{
    TotalPrice[section] = pricetoadd;
    total = 0;

    for(key in TotalPrice)
    {
        total = total + TotalPrice[key];
    }

    $("#monthlycost").html("$" + total);
    $("#firstpayment").html("$" + total);
}

<input {if $hw_s.disk[$_var].content == $__val.content}checked{/if} type=radio name="order[disk][{$_var}][content]" value="{$__val.content}" price="{$__val.price}" setup="{$__val.setup}" onclick="updatetotal('disk{$_var}', {$__val.price})">

Note that the value is very important. It has to be $__val.content for all of the backend code to function properly.

What I am trying to do is convert above to a dropdown and have the functionality still work properly. The JS function keeps a running total of every selected item and adds them together. With an input type radio it's very simple since the JS function can take a price variable. Unfortunately, with a dropdown (as far as I know), the onChange function has to go be associated with the select, not the option. Unfortunately, since the options are dynamically generated, the select doesn't know anything about the options.

Code so far (not working):

function updatetotaldd(section, elementid)
{
    var selectBox = document.getElementById(elementid);
    var selectedValue = selectBox.options[selectBox.selectedIndex].value; // does not get what I need since value is a string. I need to somehow access the price of the selected option.


    TotalPrice[section] = selectedValue;

    for(key in TotalPrice)
    {
        total = total + TotalPrice[key];
    }

    $("#monthlycost").html("$" + total);
    $("#firstpayment").html("$" + total);
}

<select class="tech" name="order[disk][{$_var}][content]" id="svtech{$_var+1}" data-enablecheckbox="false" onChange="updatetotaldd('disk{$_var}', 'svtech{$_var+1}')">
    {foreach key=__var item=__val from=$_val}
        <option {if $hw_s.disk.content == $__val.content}selected{/if} value="{$__val.content}"  price="{$__val.price}" setup="${$__val.setup}" onSelect="updatetotaldd('disk{$_var}', 'svtech{$_var+1}')">
        {$__val.content}
        {if $__val.price > 0 && $__val.setup > 0}
            <strong> - add ${$__val.price}/Mo + ${$__val.setup} One time fee</strong>
        {elseif $__val.price > 0}
            <strong> - add ${$__val.price}/Mo</strong>
        {elseif $__val.setup > 0}
            <strong>- add ${$__val.setup} One time fee</strong>
        {/if}
        </option>
    {/foreach}
</select>

How can I either pass in the price to javascript (whether it be onChange on the select, some other action on the option itself), or somehow have the option have a variable attribute (NOT VALUE)?

2
  • Can you add the second option without server side code, preferably in jsFiddle. Thanks. Commented Jun 5, 2013 at 0:53
  • jsfiddle.net/xAkVv has the requested change. Commented Jun 5, 2013 at 1:10

2 Answers 2

2

fletch is right, i corrected the errors. See here.

var selectedOption = selectBox.options[selectBox.selectedIndex]; //need to somehow access the price of the selected option.

price= parseInt(selectedOption.getAttribute('price'));
Sign up to request clarification or add additional context in comments.

4 Comments

I was playing around, and I can't figure out why trying to access .price is returning undefined. I was always under the impression that .getAttribute can be flaky at times.
I wrote that your answer is correct. And .getAttribute always works see quirksmode.
Nvm, figured it out...that element.attributeName only works for W3C defined attributes. I should have done element.attributes.attributeName.value instead. Amazing where a little documentation can take you (if I'd bother to read it)...;)
well, he should mark your answer as the accepted answer...you've provided a very clean solution in jsfiddle.
0

Just throwing this out there in case you hadn't tried...

You've defined the price attribute on the option element, so you should be able to retrieve it like any other property. Not sure you need to call onSelect on each of the option elements as the onChange on the select element should do the trick.

function updatetotaldd(section, elementid)
{
    var selectBox = document.getElementById(elementid);
    //var selectedValue = selectBox.options[selectBox.selectedIndex].value; // does not get what I need since value is a string. I need to somehow access the price of the selected option.
    var selectedPrice = selectBox.options[selectBox.selectedIndex].attributes.price.value;


    TotalPrice[section] = selectedPrice;

    for(key in TotalPrice)
    {
        total = total + TotalPrice[key];
    }

    $("#monthlycost").html("$" + total);
    $("#firstpayment").html("$" + total);
}

<select class="tech" name="order[disk][{$_var}][content]" id="svtech{$_var+1}" data-enablecheckbox="false" onChange="updatetotaldd('disk{$_var}', 'svtech{$_var+1}')">
    {foreach key=__var item=__val from=$_val}
        <option {if $hw_s.disk.content == $__val.content}selected{/if} value="{$__val.content}"  price="{$__val.price}" setup="${$__val.setup}" onSelect="updatetotaldd('disk{$_var}', 'svtech{$_var+1}')">
        {$__val.content}
        {if $__val.price > 0 && $__val.setup > 0}
            <strong> - add ${$__val.price}/Mo + ${$__val.setup} One time fee</strong>
        {elseif $__val.price > 0}
            <strong> - add ${$__val.price}/Mo</strong>
        {elseif $__val.setup > 0}
            <strong>- add ${$__val.setup} One time fee</strong>
        {/if}
        </option>
    {/foreach}
</select>

2 Comments

Unfortunately, that doesn't work. That is the reason I put the price attribute onto the option but var selectedPrice = selectBox.options[selectBox.selectedIndex].price doesn't return anything.
Yes, I fixed the error in retrieving the price attribute from the selected option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.