0

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.

2
  • You are comparing a DOM object with a number. They are different Commented Sep 23, 2014 at 7:53
  • as method suggest document.getElementById() give me element in document with that id and not its content. So you are comparing element with 2. Get the content and compare Commented Sep 23, 2014 at 7:56

6 Answers 6

3

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());
Sign up to request clarification or add additional context in comments.

Comments

0

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

try this

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

3 Comments

He is saying using Java Script and you gave him jQuery solution
@ShoaibChikate The OP is also using jQuery.
he used $('#test').html(2);
0

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')

Comments

0

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!')
    };
});

Comments

0

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!')
    };
});

Comments

0

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!')
    };
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.