I’m very new to fiddling with javascript – but here is what I’m trying to accomplish.
I have code like such:
$('#numberItems').keyup(function () {
var num = parseInt($(this).val());
var price = parseInt($('#price').text());
var.cost = num * price
$('#total').text(cost);
}).keyup();
This really works fine for what I’m doing – but what I’d like to do is take the cost variable and stick it in a link – such as:
<a href="http://www.google.com/<script>document.write(cost)</script>">Googly</a>
I realize, to make the variable global, I need to stick outside of the function, but then how do my calculations save to it?
You do not actually need to make the variable global, unless you are accessing it elsewhere. If you want the URL change to occur when a user changes the element with id numberItems, You are actually going to need to add an ID attribute to that A element, then use javascript to update the HREF attribute.
And the javascript (after your $(‘#total’).text(cost); line):
You can change the javascript to jquery as you need.