Maut1497
Maut1497

Reputation: 3

How can I compare a variable to a number in an if statement using JavaScript?

I have tried to make an if statement to check if a variable is 2. But everytime I try, I get the else statement. Here's my Javascript:

$(document).ready(function() {
    $('#test').html(2);
    var antkeepere = document.getElementById('test');

    if(antkeepere==2){
        $('#mer').html('Ja!')
    } 
    else{
        $('#mer').html('Nei!')
    };
});

First I wrote 2 in my html, but it didn't work. THen I tried to make it 2 through js as in the example over, but it didn't work either. I have searched through other threads like this and tried to but .value both in the if statement and when creating the variabel, but none of them worked either.

Upvotes: 0

Views: 377

Answers (6)

KooiInc
KooiInc

Reputation: 122888

First, you're not retrieving the html of #test but the whole element. Second, to make sure the html value is a number with value 2, you should try to convert the html value to a Number. A simple code adjustment:

$(document).ready(function() {
    $('#test').html(2);
    var antkeepere = Number(document.getElementById('test').html());

    if(antkeepere === 2){
        $('#mer').html('Ja!')
    } 
    else{
        $('#mer').html('Nei!')
    };
});

Upvotes: 0

3Dom
3Dom

Reputation: 805

Most of the answers above are pretty good, but seeing as you've already loaded jQuery, you may as well use it (you don't need document.getElement...).

$(document).ready(function() {
    $('#test').text(2); // Using text() instead of html() just means you can write stuff like "&", instead of "&"
    var antkeepere = parseFloat( $('#test').text() ); // parseFloat ensures the contents of test is converted to a number.

    if(antkeepere == 2){
        $('#mer').text('Ja!')
    } 
    else{
        $('#mer').text('Nei!')
    };
});

Upvotes: 0

Haroon
Haroon

Reputation: 127

try below code

 //As I understand test is div or span not input type objects. if it is input type element then use val() instead of html()

 $(document).ready(function() {
      $('#test').html(2);
     var antkeepere = document.getElementById('test');
    //   use can also use parseInt($('#test').html())
    if(parseInt(antkeepere.innerHTML)==2){
       $('#mer').html('Ja!')
     } 
     else{
       $('#mer').html('Nei!')
    };
});

Upvotes: 0

andrex
andrex

Reputation: 1032

You do like this in jQuery

$(document).ready(function() {
    $('#test').html(2);
    var antkeepere =$('test').html();

    if(parseInt(antkeepere) == 2){
        $('#mer').html('Ja!')
    } 
    else{
        $('#mer').html('Nei!')
    };
});

Since you are putting the 2 using .html $('#test').html(2); you can also access that using the same function .html() but without the arguments.

If comparing the antkeepere variable to 2 you need to parse it so it's will convert it into a number because .html returns a string. so that is why you need to do this if(parseInt(antkeepere) == 2)

If you are going to compare string you don't need to parseInt you can simply go like this if(antkeepere == 'aaab')

Upvotes: 0

worldask
worldask

Reputation: 1837

because document.getElementById('test'); returns a dom object.

try this

var antkeepere = $("#test").val();

Upvotes: 0

Barmar
Barmar

Reputation: 780663

document.getElementById() returns the DOM element, not its contents. To get its HTML content, you have to use the .innerHTML property. And to convert it to a number, you should use parseInt().

var antkeepere = parseInt(document.getElementById('test').innerHTML, 10);

Since you're using jQuery, you can stick with that:

var antkeepere = parseInt($("#test").html());

Upvotes: 3

Related Questions