/    Sign up×
Community /Pin to ProfileBookmark

Setting and reading value in a <span>

I just can’t get this to work for some reason.. Here is the page setup:

I have 3 <span> tags:

[CODE]<span id=”carttotal”></span>
<span id=”shipping”></span>
<span id=”ordertotal”></span>[/CODE]

When A user clicks a radio button for their shipping method:

[CODE]
<input id=’priority’ onclick=”updatetotal(‘priority’)” type=”radio” name=”shipping” value=”priority” />
[/CODE]

I have a script that should insert the shipping total in to the <span>, which it does, but what I can not get it to do is to take that value, add it to the value in the ‘carttotal’ <span> and output this to the ‘ordertotal’ <span>.

Here is my script:

[CODE]
<script type=”text/javascript”>
function updatetotal(id)
{
if (id === ‘priority’){
document.getElementById(‘shipping’).innerHTML=”5.00″; //set to $5.00 shipping
var cart_total = document.getElementById(‘carttotal’).value; // this never gets set. always comes up undefined…
var shipping = document.getElementById(‘shipping’).value;
var order_total = parseFloat(cart_total) + parseFloat(shipping);
document.getElementById(‘ordertotal’).innerHTML = order_total;
}
else if (id === ‘parcel’){
//nothing really completed here yet, testing using the priority radio button
document.getElementById(‘shipping’).innerHTML=”3.00″;
document.getElementById(‘ordertotal’).innerHTML=”5.00″;
}

}
</script>
[/CODE]

The text in the ‘ordertotal’ <span> gets set to $NaN, and I have tried many other combinations and tricks, but I always get $NaN or undefined..

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@NedalsJul 28.2010 — 
var cart_total = document.getElementById('carttotal').value; // this never gets set. always comes up undefined...
[/QUOTE]
Perhaps cart_total has not been defined
Copy linkTweet thisAlerts:
@inhauthorJul 28.2010 — It's my understanding that you can define a variable either by:

var varname

or by using the variable and setting a value to it. I played it safe and did both.. No worky =[
Copy linkTweet thisAlerts:
@tirnaJul 28.2010 — [CODE]
var cart_total = document.getElementById('carttotal').value; // this never gets set. always comes up undefined...
[/CODE]


'carttotal' is a <span> and <span> elements do not have a property called 'value'.
Copy linkTweet thisAlerts:
@inhauthorJul 28.2010 — tirna, thanks, that was the bit of nudge in the right direction I needed. here is the working bit of code:

[CODE]
document.getElementById('shipping').innerHTML="5.00"; //set to $5.00 shipping
var cart = document.getElementById('carttotal'); // this never gets set. always comes up undefined...
var cartnode = cart.firstChild;
var cart_total = parseFloat(cartnode.data);

var shipping = document.getElementById('shipping');
var shipnode = shipping.firstChild;
var shipping = parseFloat(shipnode.data);

var order_total = cart_total + shipping;
document.getElementById('ordertotal').innerHTML = order_total.toFixed(2);[/CODE]


thanks!
Copy linkTweet thisAlerts:
@tirnaJul 28.2010 — no problem ?

btw - another option you could have used is:

[CODE]
var cart_total = parseFloat(document.getElementById("carttotal").innerHTML);
[/CODE]
×

Success!

Help @inh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...