Having the toughest time writing in the function to hide numbers that are equal to or less than zero. Here is the function I want to write it in at:
function isItANumber() {
var increased = parseInt($("#increasedRevenueValue"));
if(isNaN(increased)) {
$("#increasedRevenueValue").hide();
}
}
Any thoughts? Should I use OR
?
EDIT: Here is the fiddle of my code /
Having the toughest time writing in the function to hide numbers that are equal to or less than zero. Here is the function I want to write it in at:
function isItANumber() {
var increased = parseInt($("#increasedRevenueValue"));
if(isNaN(increased)) {
$("#increasedRevenueValue").hide();
}
}
Any thoughts? Should I use OR
?
EDIT: Here is the fiddle of my code http://jsfiddle/YVcj7/
Share edited Mar 15, 2012 at 15:37 Sethen asked Mar 15, 2012 at 15:27 SethenSethen 11.4k6 gold badges38 silver badges66 bronze badges 11-
What type of element has ID
increasedRevenueValue
? – Anthony Grist Commented Mar 15, 2012 at 15:28 - 7 I think it should be $("#increasedRevenueValue").val() if it is an input element or $("#increasedRevenueValue").text() if it is span/div/any html grouping element. – Selvakumar Arumugam Commented Mar 15, 2012 at 15:29
- 1 The condition isNaN.. needs to be extended as this only takes care of non-numeric values – Chetter Hummin Commented Mar 15, 2012 at 15:31
- 2 This munity is also about thoroughly researched and properly written questions. – Sparky Commented Mar 15, 2012 at 15:43
- 2 @SethenMaleno The fiddle should support your question, not contain key/crucial pieces to it. All critical code should be pasted here. – Xyan Ewing Commented Mar 15, 2012 at 16:08
7 Answers
Reset to default 6Try the following, Im taking a guess at what element your using.
function isItANumber() {
var increased = parseInt($("#increasedRevenueValue").text());
if(isNaN(increased) || increased <= 0) {
$("#increasedRevenueValue").hide();
}
}
isItANumber();
Live Demo
And pure js.. because do we really need jQuery for this?
function isItANumber(el) {
var increased = parseInt(el.innerText);
if(isNaN(increased) || increased <= 0) {
el.style.display = 'none';
}
}
var element = document.getElementById("increasedRevenueValue");
isItANumber(element);
pure js demo
I think you want
if(isNaN(increased) || increased <= 0){
$("#increasedRevenueValue").hide();
}
I don't think you can parseInt a jQuery object. I would try parseInt($("#increasedRevenueValue").text());
or parseInt($("#increasedRevenueValue").val());
depending on what #increasedRevenueValue
is.
It looks like your function is doing a bit of double duty (getting the value of a field, checking if it is a number, etc.).
First, I would remend checking that the value is numeric. This question has a very good isNumeric function
that you can leverage.
In that case, you just need to do the following:
function isItANumber(value) {
var value = $("#increasedRevenueValue").val() || $("#increasedRevenueValue").text() //get the value if this is an input field, or fallback on the text otherwise.
var isNumber = isNumeric(value);
var isNegative = value < 0;
if (isNumber && isNegative) {
$("#increasedRevenueValue").hide();
}
}
As people have mentioned, if the element is a input field, your 'increased' should be:
var increased = parseInt($("#increasedRevenueValue").val(), 10);
If its a div/span/paragraph or something along those lines it should be.
var increased = parseInt($("#increasedRevenueValue").text(), 10);
Then simply do an if loop.
if(increased =< 0) {
$("#increasedRevenueValue").hide();
}
Depending on how your use of this function is you might need to consider if $("#increasedRevenueValue").val() is a empty value, paresint will make it into 0.
I am going to assume that $("#increasedRevenueValue")
is an input element,
function isItANumber() {
//change it to $("#increasedRevenueValue").text() if it is div/span
var increased = parseInt($("#increasedRevenueValue").val(), 10);
if(isNaN(increased) || increased <= 0) {
$("#increasedRevenueValue").hide();
}
}
I would try: http://jsfiddle/pratik136/6J7xz/