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 6949399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:56:57+00:00 2026-05-27T13:56:57+00:00

I am new with JavaScript, I have a value in an input field, like

  • 0

I am new with JavaScript, I have a value in an input field, like 0 or 1 and then when this value changes a word is changed to ‘Off’ or ‘On’ respectively.

I know this is pretty simple but I’m new with JavaScript as I said.

How does your code look like currently?
What have you tried so far?

EDIT
I’m working in the code right now so nothing great so far:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

<script type="text/javascript">
    function change(value) {
        if (value == 1)
            return "ON";
        else
            return "OFF";
    }
</script>

</head>
<body>

    <input type="text" value="1" />
    <p>ON OR OFF RIGHT HERE</p>

</body>
</html>

What difficulties are you encountering?

I need this values keep been updating all the time, if the value of the input field change the word must change either.

If you are new to javascript what tutorials/articles/books did you read so far in order to get started?

Not a specific tutorial right now, I’m googling.

EDIT 2

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

<script type="text/javascript">
    function change(){
        toggler = document.getElementById('toggler');
        onoff = document.getElementById('onoff');
        toggler.onChange = function(e){
            onoff.innerHTML = (toggler.value==1)? 'on' : 'off';
        }
    }
</script>

</head>
<body>

    <input type="text" id="toggler" value="1" onkeyup="change()" />
    <div id="onoff"></div>

</body>
</html>

I’m trying to following the suggestions but still not working, what am I doing wrong here guys ?

  • 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-27T13:56:58+00:00Added an answer on May 27, 2026 at 1:56 pm

    You don’t posted any code but here it goes some general code:

    Your input:

    <input type='text' id='myinput' onkeyup='contentChanged(this)' />
    

    Look for other key events: http://unixpapa.com/js/key.html

    Then, your function:

    function contentChanged(myinput)
    {
        var myvalue = myinput.value;
    
        if (myvalue == "1")
        {
            // Do something with value = 1
        }
        else if (myvalue == "0")
        {
            // Do something with value = 0
        }
        // And so on...
    }
    

    EDIT:

    Now that you’ve posted your code, you can do like as I said:

    <input type="text" value="1" onkeyup="change(this.value)" />
    

    EDIT 2:

    You’re setting two events to your object and onChange just works to select object. Because of this I suggested you to use onkeyup or another key event. Just remove your onChange event out of your function.

    Change to this and try:

    function change(){
        toggler = document.getElementById('toggler');
        onoff = document.getElementById('onoff');
        onoff.value = (Number(toggler.value)==1)? 'on' : 'off';
    }
    

    Number function is important to cast your input data and be sure that is a number.
    Plus add an maxlength attribute on your textfield to limit user input data:

    <input type="text" id="toggler" value="1" onkeyup="change()" maxlength="1" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used Javascript onlaod like this: function check() { var pic = new
I want my form to have a field with a pre-fixed value like this:
I would like to write in an input field with a PHP variable. This
I'm quite new to javascript but have undertaken a task to get better aquainted
I have opened a new window with JavaScript: var newwin = window.open('','preview','width=600,height=500'); Now I
i have a javascript method that takes a date: convert(new Date(02/20/2010); how can i
I have some code that dynamically creates a new button through JavaScript that when
I have just started learning Jquery and am new to writing javascript (I am
Wondering how to open many new windows with Javascript. I have found plenty of
I have an array in Javascript: var array = new array(); array[0] = apples;

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.