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 resultingNaN
:P – Alvin Wong Commented Jul 27, 2012 at 15:14 -
2
It's because a) you're looking for ids
WeeklyCarSales
andDailyCarSales
, but the spans have idsWeekly
andMonthly
, 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
3 Answers
Reset to default 7This 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
版权声明:本文标题:jquery - Storing a span value into a javascript variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741288509a2370413.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论