Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6647079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:29:41+00:00 2026-05-26T00:29:41+00:00

I got a question why my if-else statement is not working. I got a

  • 0

I got a question why my if-else statement is not working.

I got a form with a text input, and when a user types in the size they want it should live edit the picture. I’m using the addclass and removeclass methods from jQuery. This is my script:

function slide() {
    var n = Number(document.getElementById('Getal1').value)

    if (n >= 101 && n < 201)
        $(openDiv1());

    else if(n > 200)
        $(openDiv2());

    else if(n >= 0 && n < 101)
        $(closeDiv());

    else {
        alert("You did not enter a number!")
    }
}

$(function openDiv1() {

    $( "#ombouw_wrap" ).addClass( "Addclass_ombouw_wrap_2block", 1, callback );
    return false;
});

function callback() {
    setTimeout(function() {
        $( "#midden" ).removeClass( "Addclass_ombouw_wrap_1block Addclass_ombouw_wrap_3block" );
    }, 0 );
}

$(function openDiv2() {

    $( "#ombouw_wrap" ).addClass( "Addclass_ombouw_wrap_3block", 1, callback );
    return false;
});

function callback() {
    setTimeout(function() {
        $( "#midden" ).removeClass( "Addclass_ombouw_wrap_1block Addclass_ombouw_wrap_2block" );
    }, 0 );
}

$(function closeDiv() {

    $( "#ombouw_wrap" ).addClass( "Addclass_ombouw_wrap_1block", 1, callback );
    return false;
});

function callback() {
    setTimeout(function() {
        $( "#midden" ).removeClass( "Addclass_ombouw_wrap_2block Addclass_ombouw_wrap_3block" );
    }, 0 );
}

window.onload = closeDiv;

And this is my HTML code:

Lengte: <INPUT TYPE="text" NAME="numeric" onKeyup="slide()" id="Getal1" onKeyPress="return checkIt(evt)">
x Hoogte: <input type="text" ID="Getal2" onKeyPress="return checkIt(evt)">
x Diepte: <input type="text" ID="Getal3" onKeyPress="return checkIt(evt)">
</form>

When you reload the page, you see it is not starting with closedivs, but it starts straight with adding classes without executing the if-else statement.

What am I doing wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:29:42+00:00Added an answer on May 26, 2026 at 12:29 am

    When you reload the page, you see it is not starting with closedivs, but it starts straight with adding classes without executing the if-else statement.

    This is because jQuery treats $(function) as a handy form of $.ready(function): the function-object passed is executed on page load.

    That is, $(function openDiv1() {...}) defines a function-object which is then passed to jQuery to execute automatically when the page loads. Instead, remove the functions from $(...). For instance, function openDiv1() {...} will suffice. (This will also fix the errors with not being able to call openDiv1 — since it is a function-expression it will not be assigned to a variable/property in scope.)


    The following describes an issue with the slide function.

    JavaScript does some coercions; the following show why the else branch will never be reached:

    Number("")         // same as Number(0)
    Number("") >= 0    // true
    

    But really, please don’t use Number (it’s a wrapper-object for number), consider parseInt(str, 10) as a replacement. As an added bonus, parseInt("", 10) evaluates to NaN (not 0) and since NaN >= 0 is false, this will fix the initial problem.

    function slide(){
        var n = parseInt(document.getElementById('Getal1').value, 10)
        if (n >= 0 && n <= 100) {
            $(closeDiv())
        } else if (n > 100 && n <= 200) {
            $(openDiv1())
        } else if (n > 200) {
            $(openDiv2())
        } else {
            alert("You did not enter a number!")
        }
    }
    

    Also pay attention to the consistency changes I added. The brackets are not necessary, but I find it aides in writing in a consistent well-formatted/well-indented style. Other things to pay attention to are ordering of if/else statements as well as the comparissons (keep it “flowing”).

    Happy coding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a question ;) Actually I want to use core-data in different threads.
I've got a table with a date column, where a user input will be
This if else statement does not work: (in this situation it displays 'x is
I got a question regarding my MySQL-database and would like to get input on
I've got a question regarding jQuery keypress events. I've got the following (working) code:
I posted a question about how to get user input such as YES or
I've got question about wpf xaml style definitions. When I try to set style
This question got me thinking about bare strings. When PHP sees a string that's
Just got a question about generics, why doesn't this compile when using a generic
I got this question from this discussion . A method call like object.m does

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.