When the 'object' value is 0, the 'level' input will not appear on the screen

Task at hand:
Hide the 'level' input when the 'object' value is 0.

Current situation:

input type="text" id="level"                                          
input type="text" id="object"                     

My attempt:

var lvl = document.getElementById("level")
var ogg = document.getElementById("object")

if (ogg == 0) {
    lvl.style.display = 'none';

};

I am struggling to get this code to work as I am new to JavaScript. Any assistance would be greatly appreciated, thank you!

Answer №1

ogg represents an HTML element that requires the value property of ogg to access the value of the input.

if ( ogg.value == 0) {
        lvl.style.display = 'none';

    };

If you need to check if the value is null or empty:

if (!ogg.value && ogg.value.trim() == "") {
    lvl.style.display = 'none';

};

Answer №2

To retrieve the input field's value, utilize the .value attribute as demonstrated below:

if (ogg.value == 0) {
    lvl.style.display = 'none';
};

Answer №3

Obtain the element by using var ogg = document.getElementById("object")
. This will give you access to the element.

To retrieve the value of the input element, simply use ogg.value.

Additionally,

You will receive the value as a string from the input element.

If you expect the input to be an integer or number, it is recommended to parse it into an int like this:

var value = parseInt(ogg.value)

You can now use this parsed value in conditional statements like so:

if(value === 0)

Answer №4

Follow these steps:

let value = document.getElementById("object").value;

The .value attribute allows you to retrieve the text value of the HTML element. Do you have any other inquiries?

For more details:Check here

Furthermore, you can modify the text value of an element using .value

document.getElementById("myOption").value = "newText";

Answer №5

To properly validate the value of object, you need to use the keyup event. Here is a functional example:

var level = document.getElementById("level");
var object = document.getElementById("object");

function checkValue(){
  if (parseInt(object.value) === 0) {
      level.style.display = 'none';
  }
  else {
      level.style.display = 'block';
  }
}
<input type="text" id="level" placeholder="Level">
<input type="text" id="object" placeholder="Object" onkeyup="checkValue()">

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Attaching event handlers to changing elements.. great for responding to clicks, but not for mouse hovering

I've been diving into a new project that involves using handlebars to generate dynamic content. Take a look at this example http://jsbin.com/hahiv/2/edit I'm puzzled as to why the click event is functioning correctly but the hover effect is not. ...

utilizing jQuery to iterate through JSON data with a random loop

Is there a way to modify this code to display only one image randomly selected from a JSON file that contains multiple images? Here is the code in question: $(document).ready(function() { $.getJSON('https://res.cloudinary.com/dkx20eme ...

Enhancing a Stripe customer object with additional metadata

While setting up a payment system using Stripe, I encountered an issue when trying to add metadata to the customer object. Specifically, I wanted to include my workspace id in the metadata property of the customer. However, upon executing the code below, I ...

Sending a PHP variable to a JavaScript function

When attempting to pass a value to a JavaScript function by clicking a link, I encountered an error message: Uncaught SyntaxError: Unexpected identifier The following is the code snippet: <?php include_once("php_includes/check_login_status.php") ...

Looking for a way to assign the object value to ng-model within a select tag from an array of objects? Also, curious about how to easily implement filters on ng-options?

Here is the HTML code for creating a question template: <body ng-controller="myCtrl"> {{loadDataSubject('${subjectList}')}} {{loadDataTopic('${topicList}')}} <h1 class = "bg-success" style="color: red;text-align: ...

unable to detect image input type

My dilemma lies in trying to submit a form using an image input type. The ajax request works as expected, and I receive a response from my php script. However, I encountered an issue where the image button was not changing its image upon submission. Accord ...

Sending an array of integers to Django using Ajax

I'm currently working with DataTables and have implemented a feature that allows users to select multiple rows and delete them. As of now, the functionality deletes only the first selected row using the following code snippet. Ajax Code: /* Clic ...

Developing a unique dark theme for bootstrap without the need for redundant CSS coding

Currently, I am exploring the most effective way to structure my SCSS dark theme for optimal maintainability. Bootstrap 5 offers the option of setting the data-bs-theme='dark' attribute on the HTML body tag to switch between light and dark themes ...

php-ews: Fatal error: Unhandled SoapFault exception: [Client] The class 'EWS_Exception' is not found

Issue with Exchange Server Code For the past year, my code has been running smoothly without any problems. However, yesterday, I encountered an error message that disrupted its functionality. The code is designed to retrieve the number of unread emails in ...

Excruciatingly tardy performance from Node, Apollo, and Sequelize (over 7 seconds)

Although I'm more comfortable with Laravel, I am experiencing a delay of approximately 7.2 seconds when running a single query in Apollo Server for around 300 items. The resolver code provided below seems to be fairly straightforward and only involves ...

Looking for assistance with CSS to properly align a dozen items

UPDATE: See the current layout in this JFiddle based on feedback received from others, still a few tweaks needed. In my setup, there are 12 divs housed within one container div. These 12 divs consist of 6 text components and 6 corresponding input fields. ...

Dynamic Loading of View and Controller in Angular JS using ui-router

Is there a way to dynamically load both a view and its corresponding controller in my application? I anticipate having multiple views and controllers in my app, and I would prefer not to load all controller definitions during application setup. Instead, I ...

What is the best way to transform JSON into a Java object?

I am encountering an issue with loading the image while trying to convert a JSON into a JAVA Object from this specific link. Below is the code snippet I have been working on: // code snippet goes here Although everything seems to be functioning corre ...

Ensuring Form Accuracy - Mandatory Selection from Group

Currently, in the project I am working on, there are three textboxes that need to be validated to ensure at least one of them has been filled out. I have been researching custom validation with Angular directives and found that it is possible to set the v ...

Is there a way to retrieve all documents based on the start and end of a specific day?

One common issue I am facing involves submitting a date to my nodejs server in a specific format

 2018-11-02T00:36:00+05:30 // The actual time should be 12:36AM However, when examining the document in my database (using studio 3T), the format appear ...

What could be the reason for this function failing to calculate the mean of a set of data points?

I'm facing a challenge with this beginner problem. "A task for you: Calculate the average score of a class whose test scores have been graded by a teacher. Your mission is to complete the getAverage function, which receives an array of test sco ...

What steps can I take to deactivate input and stop it from being accessible on the browser?

insert image description Is there a method to block users from accessing disabled input fields in the browser? Any recommendations or solutions would be greatly appreciated. Vuejs is utilized within my project. Implementing this feature serves as a secu ...

What is the best method for retrieving the complete error message from my client-side Node and Express server?

When I send a request to my express route and it returns a 400 status along with an error message, I am facing an issue on the client-side. The alert message only displays "Object object" instead of the actual error message that I see on the server side. U ...

Using Thymeleaf within Javascript code to generate a URL?

Here is my question: The project's base URL is : The test page URL is :, and on this page, I have included a JavaScript file called datatable.packer.js using the following code: <script type="text/javascript" th:src="@{/resources/js/datatable ...

The process of departing a SocketIO room and switching to a different room logic

I am wondering how I can leave the Room when I click on a new Room Here is what my page looks like: https://i.sstatic.net/vuwv0.png The list on the left side is from the MySQL Server and it displays a list of my chats. Each Room name has an id value whi ...