JavaScript still displaying empty values despite correct syntax being used

Here is the HTML form I have created:

<form accept-charset="utf-8" onsubmit="return validateForm()">
    <input placeholder="Enter Username" type="text" id="user">
    <input placeholder="Enter Your Password" type="password" id="pass">
    <button>Login</button>
</form>

This is the JavaScript code included in the head section:

<script type="text/javascript">
    function validateForm() {
        var username = document.getElementById("user").textContent;
        alert(username);
        return false;
    }
</script>

I am facing an issue with the code. Even though my syntax is correct, when I enter a value in the username field and click the login button, the JavaScript code displays a blank value. This code was working fine yesterday, but today it's not showing the input values. Can someone help me understand why this is happening?

Answer №1

function verifyUser()
        {
            var username = document.getElementById("user").value;
            alert(username);
            return false;
        }
<form accept-charset="utf-8" onsubmit="return verifyUser()">
    <input placeholder="Enter Username" type="text" id="user">
    <input placeholder="Enter Your Password" type="password" id="pass"/>
    <button>Login</button>
</form>

Answer №2

It's recommended to utilize the value property rather than InnerHTML.

Answer №3

Give this a shot

   let userInput = document.getElementById("user").value;

innerHTML is solely for writing content on the screen.

Answer №4

To retrieve the values entered inside an input field, use document.getElementById("user").value;.

If you want to access the text content of an element, you can use document.getElementById("user").innerHTML. For example, this will display "SAMPLE TEXT".

Remember to use document.getElementById("sampletxt").innerHTML to actually display the text "SAMPLE TEXT".

I hope this information is helpful to you!

Answer №5

<input> elements are unique in that they are self-closing, meaning they do not have an innerHTML property. Instead, you can retrieve the value of the input field using the value property.

It all makes sense when you stop and think about it, but just wait until you dive into the world of <textarea> elements...

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

Delaying Jquery on scroll Event

Hey there, I've got a set of icons that I'd like to reveal one by one as you scroll down. I've incorporated some animations from , but now I'm wondering how I can implement a delay function in my jQuery so the icons appear sequentially ...

What could be causing my form not to Submit when attempting submission without utilizing the submit button?

Here is the code for my validation: $(function () { function validateForm() { // Code to validate form inputs } $('#myform').submit(validateForm); }); After this, I want to submit the form when a certain action occurs: < ...

Striving to design an event planner with the datepicker feature in javascript

Currently, I am integrating a javascript datepicker into my project to facilitate displaying a calendar and selecting a date for adding/viewing/editing events on that specific date. On my view, the datepicker calender is displayed within a div element. & ...

Version 2.7.2 of Chart.js now allows users to retrieve the value of a data point by

I need to retrieve the value for each point clicked on, but I am currently only able to get the value of the first line when I click on a point. The GetElementsAtEvent function provides me with an array of 3 active elements, but how can I specifically extr ...

Ensuring the accuracy of user input in JavaScript, even after fixing a mistake and submitting again, will not cause the code to exit the else clause

I am currently working on a coupon code field that needs to accept a specific set of coupon keys. Below is the code I have written for validation. If a user enters an invalid key at first, they receive an alert saying "Invalid Key." However, if they corr ...

AngularJS tree grid component with customizable cell templates

I have been utilizing the tree-grid component in AngularJS from this link: Here is an example of it on Plunker: http://plnkr.co/edit/CQwY0sNh3jcLLc0vMP5D?p=preview In comparison to ng-grid, I am unable to define cellTemplate, but I do require the abilit ...

What are some ways to implement the 'charCodeAt' method within a for loop effectively?

Currently, I am working on developing a hangman game in JavaScript to enhance my coding skills. One task that I am facing is extracting the keyCode of a character at a particular position within an array. To achieve this, I have been using the 'charCo ...

The value of a select box cannot be retrieved until it has been clicked on

I am working with a selectBox element in my code: <select class="span2" name="filterYear" id="filterYear" style="margin-right:10px;"> <% for (var i = 0; i < years.length; i++) { %> <% if (years[i] == selectedYear) { %> ...

Determine the presence of an element using its selector and retrieve a true or false value using jQuery or JavaScript

Is there a way to determine if an object is present on the page based on its selector? This method typically works: startpageentry = $('#' + startpageid) However, it does not return anything. I require a boolean value that can be used in an if ...

Place the Material-UI Drawer component beneath the Appbar in the layout

Currently, I am working on developing a single-page application using Material-UI. In this project, I have integrated the use of an AppBar along with a ToolBar and a Drawer. However, I have encountered an issue where the Drawer is overlapping the AppBar an ...

Using the getAttribute method in Edge with JavaScript

My goal is to dynamically load videos on the page after it has fully loaded. I have a script that successfully works in Firefox and Chrome, but I encounter errors when using Edge/IE. The specific error message I receive is SCRIPT5007: Unable to get propert ...

Utilizing Ajax to serialize or transfer JSON objects

I have received a Json object and I am looking to extract the data using JavaScript. Specifically, I need help with looping through fields and extracting the data. def For_Sale_Listing(request,id): try: listing = Listing.objects.filter(pk=id) ...

The image file that was uploaded to our S3 storage has been detected

I'm attempting to upload an image created by cropperjs to a DigitalOcean space. To achieve this, I am utilizing a pre-signed URL and performing a put request using Axios. The problem arises when I try to open the uploaded image, as it appears to be ...

Ways to activate the built-in HTML5 form validation without actually submitting the form

I have a form in my application that I plan to submit using AJAX. However, I would like to implement HTML5 for client-side validation. Is it possible to trigger form validation without actually submitting the form? Here is an example of the HTML code: &l ...

cancel button in confirmation dialog is malfunctioning

Having an issue with a button that triggers a confirmation dialog: <asp:Button ID="btnSignOff" runat="server" CssClass="button" Text="Sign OFF" OnClick="btnSignOff_Click" OnClientClick="return confirm('Are you sure you want to Sign OF ...

Save the user input to a dedicated text file

I am working with a couple of select tags that generate an array. Previously, I was only logging the array to the console upon pressing the SUBMIT button, but now I want to save it to a separate text file. Here is my main.html code: <form method="POS ...

jQuery is malfunctioning following an AJAX request

Can anyone help me fix an issue with my jQuery code? I have a hidden div that should be shown when the mouse hovers over a sibling element. However, it seems like after my AJAX function is executed, the jQuery stops working. <div class="parent"> ...

The "Read more" feature is not functional on mobile devices

I am encountering issues with my Wordpress blog on mobile screens. The "read more" button is not functioning, and the screen size does not fit properly on mobile devices. Visit abood250.com for more information. CSS Styling: /* =Global ----------------- ...

The :before element is not displaying when using jQuery's slideToggle() method

When using only CSS, the .nav-main:before{} element is displayed, but with jQuery, it does not toggle that element and always keeps it hidden. Disclaimer: This issue has been observed on mobile devices (tested in Android Chrome). jQuery $('.menu-to ...

What is the solution using high-order functions?

I am struggling with a problem I came across on the internet. The task at hand is to identify all unique elements in an array that are less than 10, calculate the sum of these elements, and then multiply each element in the array by this sum. I have heard ...