0

sorry im noob on JS i need help i want to try make a textbox checker that can be check the value of the textbox. when it has a value higher than 0 then has a value = 1, but when it has null value or 0 value then it has a value = 0

this my code

// JavaScript Document

    function check()
        {
            if (document.getElementById('s1').value=="0" 
             || document.getElementById('s1').value==undefined)
            {

                return 0;
                document.getElementById('avr').value = result;
            }
            return 1;
                document.getElementById('avr').value = result;

        }
4
  • 1
    The .value will be a string. If the field is empty it will be "", not null or undefined. Commented Feb 8, 2017 at 2:17
  • A return statement exits the function immediately, so the ...('avr').value = result lines in your code can never be executed. Also, if those lines were before the return statements you'd still have a problem because result is not defined (at least, not in the code shown). Commented Feb 8, 2017 at 2:18
  • null == 0 and any number higher than 0 == 1? Seem like a boolean value to me, Not sure what you are trying to achieve but I would use Hidden field type checkbox and create a listener to that textbox Commented Feb 8, 2017 at 2:21
  • thx guy's i'm just figure it out,. and i'm just solve my problem a min ago Commented Feb 8, 2017 at 2:27

1 Answer 1

2

You need to set the element's value before you return. A return statement terminates the execution of a function:

function check()
{
  if (document.getElementById('s1').value === '0' || document.getElementById('s1').value === '') {
    document.getElementById('avr').value = result;
    return 0;
  } else {
    document.getElementById('avr').value = result;
    return 1;
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

The test for undefined won't work. Should be a test for an empty string.
Sure, just giving the basic answer to his problem.

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.