Skip to content Skip to sidebar Skip to footer

Storing A Span Value Into A Javascript Variable

I am trying to write javascript that will go through a span, grab its value, and store it in a variable that can be used to perform arithmetic. Copy

​​Here is working JSFiddle

Solution 2:

Try

functiondivide(n1, n2) {
    ans = n1 / n2;
    document.write(" " + ans + "<BR>");
    return ans;
} 
var a  = $('#Weekly').html().replace('*','');
var b = $('#Monthly').html().replace('*','');
divide(a, b);

Solution 3:

There are a number of problems here. First, in jQuery the ('#SOMETHING') construct references a DOM element by ID. So, You would need to use $('#Weekly') and $('#Monthly') to reference the span elements.

Second, the ServerData class value that is applied to the tag is not an available property on the element so trying to reference $('#Monthly').ServerData isn't going to do anything for you.

What you are really looking for is the HTML contents of the node which you would access like this

var a = parseInt($('#Weekly').html());

I am not sure if those asterisks are actually there or not. If they are truly there you need to remove them like this:

var a = parseInt($('#Weekly').html().replace('*', ''));

Post a Comment for "Storing A Span Value Into A Javascript Variable"