0

I'm really at a loss here. I have the following code:

function AddHeightToBottom(footerHeight)
{
    if ($('#myId1').length)
    {
        $('#myId1').height($('#myId1').height() + footerHeight);
    }
    console.log(('#myId2').length);
    if ($('#myId2').length > 0)
    {
        var dHeight = $('#myId1').height();
        var aHeight = $('.AF').height();
        $('#myId1').height(dHeight + aHeight);
        var newHeight = $('#myId1').height();
        alert(newHeight);
    }
    else
    {
        alert('why?!??????');
    }
}

$('#myId2').length is coming out to 1, as I'd expect. However, when it does the comparison

if(1 > 0)

it's doing thing 2 EVERY time and I have no idea why. Any help would be appreciated.

29
  • It's doing thing 2. I would expect it to do thing 1 Commented May 19, 2016 at 20:50
  • It seems to work as expected. Can you create an example that exhibits this behavior? Commented May 19, 2016 at 20:52
  • 1
    You are using jquery, so make sure you are running it in document.ready: $( document ).ready(function() { if($('#element').length > 0) { //do thing 1 } else { //do thing 2 } }); Commented May 19, 2016 at 20:55
  • 1
    The code shown works as expected, which means there is something about the rest of the code that isn't working as you expect it to do. You need to post enough of your code to enable us to help you, but the if/else is not, itself, broken or misbehaving. Commented May 19, 2016 at 20:57
  • 1
    @SeanSmyth Look. You need to give us code that we can verify fails. The code you have posted is not enough to recreate the bug. We need enough to recreate the bug. If we cannot recreate the bug then we cannot confirm why it's failing for you. Commented May 19, 2016 at 20:59

2 Answers 2

5

You are using jquery, so make sure you are running it in document.ready:

$( document ).ready(function() { 
   if($('#element').length > 0) { //do thing 1 } 
   else { //do thing 2 } 
});

If you are calling this in another function, then that function must appear in document.ready. A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute. Code included inside $( window ).load(function() { ... }) will run once the entire page (images or iframes), not just the DOM, is ready. * Quoted from link below.

https://learn.jquery.com/using-jquery-core/document-ready/

function AddHeightToBottom(footerHeight)
{
    if ($('#myId1').length){
        $('#myId1').height($('#myId1').height() + footerHeight);
    }
    console.log(('#myId2').length);
  
    if ($('#myId2').length > 0)
    {
        alert("See, it works...");
      
        var dHeight = $('#myId1').height();
        var aHeight = $('.AF').height();
        $('#myId1').height(dHeight + aHeight);
        var newHeight = $('#myId1').height();
        alert("New height: " + newHeight);
    }
    else
    {
        alert('why?!??????');
    }
}

$( document ).ready(function() { 
  var footerHeight = 25;
  AddHeightToBottom(footerHeight);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div id="myId1">div with id of myId1</div>
<br>
<div id="myId2">div with id of myId2</div>

Sign up to request clarification or add additional context in comments.

2 Comments

Edited response to show it working. Perhaps you forgot to load jQuery? Are you including it twice? It's something related to jQuery in my opinion.
Try document.getElementById("myId2").length if you want to confirm it's related to your jquery setup.
0

The element I was trying to grab was being loaded in an iFrame, so document.ready() wasn't catching it. I don't know why Firebug was telling me the length was 1 when it was actually 0.

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.