admin管理员组

文章数量:1279018

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.

<span id ="Weekly" class="ServerData" data-tag="WeeklyCarSales">**30**</span>
<span id ="Monthly" class="ServerData" data-tag="DailyCarSales">**6**</span>

Pertaining to the above two lines, my function is given below. In this current set up I get no result.

function divide(n1, n2) {
    ans = n1 / n2;
    document.write(" " + ans + "<BR>");
    return ans;
}
var a = $('#WeeklyCarSales').ServerData;
var b = $('#DailyCarSales').ServerData;
divide(a, b);

I should be getting an answer of "5" yet get nothing. I know the actual arithmetic works if I force an integer/float value into the variables. I seem to have continuous trouble locking down the the '30' and '6', which are the span values. Any ideas on how to grab those 2 span values?

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.

<span id ="Weekly" class="ServerData" data-tag="WeeklyCarSales">**30**</span>
<span id ="Monthly" class="ServerData" data-tag="DailyCarSales">**6**</span>

Pertaining to the above two lines, my function is given below. In this current set up I get no result.

function divide(n1, n2) {
    ans = n1 / n2;
    document.write(" " + ans + "<BR>");
    return ans;
}
var a = $('#WeeklyCarSales').ServerData;
var b = $('#DailyCarSales').ServerData;
divide(a, b);

I should be getting an answer of "5" yet get nothing. I know the actual arithmetic works if I force an integer/float value into the variables. I seem to have continuous trouble locking down the the '30' and '6', which are the span values. Any ideas on how to grab those 2 span values?

Share Improve this question edited Jul 27, 2012 at 15:12 Naftali 146k41 gold badges247 silver badges304 bronze badges asked Jul 27, 2012 at 15:10 J.C.MorrisJ.C.Morris 8033 gold badges13 silver badges27 bronze badges 4
  • Are the values in your spans actually surrounded with asterisks? – j08691 Commented Jul 27, 2012 at 15:13
  • See the console and if you find any division by zero... oh sorry, 0/0 should be resulting NaN :P – Alvin Wong Commented Jul 27, 2012 at 15:14
  • 2 It's because a) you're looking for ids WeeklyCarSales and DailyCarSales, but the spans have ids Weekly and Monthly, and b) ServerData is not a property of a jQuery object. Did you mean $( '#Weekly' ).text() and $( '#Monthly' ).text()? See jsfiddle/gdWta – JJJ Commented Jul 27, 2012 at 15:16
  • no they are not in asterisks I was trying to make them bold in the post (kinda new here, still learning the formatting) – J.C.Morris Commented Jul 27, 2012 at 15:24
Add a ment  | 

3 Answers 3

Reset to default 7

This should do the trick...

function divide(n1, n2) {
    var ans = n1 / n2;
    document.write(" " + ans + "<BR>");
    return ans;
}

var a = parseInt($("#Weekly.ServerData").text().replace(/\*/gmi, ""), 10)
  , b = parseInt($("#Monthly.ServerData").text().replace(/\*/gmi, ""), 10);

divide(a, b);

​​Here is working JSFiddle

Try

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

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('*', ''));

本文标签: jqueryStoring a span value into a javascript variableStack Overflow