0

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.net/YVcj7/

Sethen
  • 11,140
  • 6
  • 34
  • 65
  • What type of element has ID `increasedRevenueValue`? – Anthony Grist Mar 15 '12 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 Mar 15 '12 at 15:29
  • 1
    The condition isNaN.. needs to be extended as this only takes care of non-numeric values – Chetter Hummin Mar 15 '12 at 15:31
  • 2
    This community is also about thoroughly researched and properly written questions. – Sparky Mar 15 '12 at 15:43
  • I would say I described what I am after pretty thoroughly. There is even a fiddle to go with it. – Sethen Mar 15 '12 at 15:45
  • Why wouldn't a simple `<= 0` work? – Sparky Mar 15 '12 at 15:45
  • @Sparky672 It did work, but you must not have looked at the fiddle. That was part of the answer but not the whole solution. – Sethen Mar 15 '12 at 15:58
  • 2
    @SethenMaleno The fiddle should support your question, not contain key/crucial pieces to it. All critical code should be pasted here. – Xyan Ewing Mar 15 '12 at 16:08
  • @XyanEwing That's a more useful answer to the downvotes I am getting. – Sethen Mar 15 '12 at 16:09
  • @SethenMaleno Haha, that's what I'm here for. I can't speak for the downvoters, but the reason for my comment is that if jsfiddle is down (godforbid), we'll still need a way to look at/access your code to help you. – Xyan Ewing Mar 15 '12 at 16:13
  • @XyanEwing Makes sense. I appreciate the comment. I am open to criticism, but receiving down votes with no reason given is frustrating. – Sethen Mar 15 '12 at 16:14

7 Answers7

6

Try 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

Loktar
  • 34,764
  • 7
  • 90
  • 104
  • Here is a fiddle of what I am doing maybe you can shed some light in this context: http://jsfiddle.net/YVcj7/ – Sethen Mar 15 '12 at 15:39
  • @SethenMaleno The #increasedRevenueValue is an input field.. You should use .val() to get the value of the input field. – Selvakumar Arumugam Mar 15 '12 at 15:40
  • For some reason, every time I try to find the value and write and if statement for it being less than 0, it's not working out. – Sethen Mar 15 '12 at 15:42
  • 1
    @SethenMaleno http://jsfiddle.net/loktar/YVcj7/1/ in the last function where you do the checkbox click event move `isItANumber()` to the bottom. You were checking the old value before you updated the new value. Also I changed `text()` to `val()` – Loktar Mar 15 '12 at 15:43
  • @Loktar That was it, bravo. I guess I still don't really understand the logic behind this. I get that it was checking the old value, but why did we have to put val in there? – Sethen Mar 15 '12 at 15:50
  • `val` refers to an input elements data. `text` is used for divs/spans/etc. – Loktar Mar 15 '12 at 15:52
  • @Loktar I get that, I am just wondering why we have to `parseInt()` that value? – Sethen Mar 15 '12 at 15:59
  • @SethenMaleno basically just to turn it into a number, otherwise it will be a string. – Loktar Mar 15 '12 at 16:04
0

I think you want

if(isNaN(increased) || increased <= 0){
    $("#increasedRevenueValue").hide();

}
devstruck
  • 1,497
  • 8
  • 10
0

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.

Paul Sham
  • 3,185
  • 2
  • 24
  • 31
0

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 recommend 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();
    }
}
Community
  • 1
  • 1
NT3RP
  • 15,262
  • 9
  • 61
  • 97
0

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.

ninja
  • 2,233
  • 1
  • 15
  • 15
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();
   }

}
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
0

I would try: http://jsfiddle.net/pratik136/6J7xz/

bPratik
  • 6,894
  • 4
  • 36
  • 67