Once a form is retrieved using AJAX, the functionality associated with the form becomes disabled

After creating a basic login form with an option for users to register, I encountered an issue when loading the registration form using AJAX. The behavior I had scripted for the login form wasn't maintained even though I used identical class names...

Below is the AJAX request:

var displayRegisterForm = function() {
    var regLink = document.getElementById("signInBoxFooter");
    regLink.addEventListener("click", function(){
        var request = new XMLHttpRequest();
        request.open("GET", "register.html");
        request.onreadystatechange = function(){
            if (request.readyState === 4 && request.status === 200){
                document.getElementById("signInBody").innerHTML = request.responseText;
            } else {
                document.getElementById("signInBody").innerHTML = "Whoops something went wrong!"
            }
        };
        request.send(null);
    },false);
}();

Regarding the form behavior:

var textInput2 = function(){
var inputs = document.getElementsByClassName("inputNoFocus");
for (i = 0; i < inputs.length; i++){
    var input = inputs[i];
    var busy = false;
    input.addEventListener("focus", function(e){
        var defaultValue = this.getAttribute("value");
        if(this.value === defaultValue){
            this.value = "";
            this.setAttribute("class", "inputFocus");
        }
        if(defaultValue === "Password"){
            this.setAttribute("type", "password");
            this.setAttribute("class", "inputFocus");
        }
        this.parentNode.parentNode.setAttribute("class", "divFocus");
        busy = true;
    }, false);

    input.addEventListener("blur", function(e){
        var defaultValue = this.getAttribute("value");
        if(this.value === ""){
            this.value = defaultValue;
            this.setAttribute("class", "inputNoFocus");
        }
        if(this.value === "Password"){
            this.setAttribute("type", "text");
            this.setAttribute("class", "inputNoFocus");
        }
        this.parentNode.parentNode.setAttribute("class", "divNoFocus")
        busy = true;
    },false);
}
}();

The HTML content within "register.html" file:

<div class="boxBody">
    <form id="regForm">
        <div id="regTop"></div>
        <div class="divNoFocus">
            <label><input class="inputNoFocus" type="text" value="Email Address"></label>
        </div>
        <div class="divNoFocus">
            <label><input class="inputNoFocus" type="text" value="First Name"></label>
        </div>
        <div class="divNoFocus">
            <label><input class="inputNoFocus" type="text" value="Surname"></label>
        </div>
    </form>
</div>

Previously contained HTML (before loading register.html):

<div id="signInBody" class="boxBody">
    <form id="login">
        <div class="divNoFocus">
            <label><input class="inputNoFocus" type="text" value="Username or Email"></label>
        </div>
        <div class="divNoFocus">
            <label><input class="inputNoFocus" type="text" value="Password"></label>
        </div>
    </form>
</div>

Answer №1

It appears that the form behaviour needs to be re-bound after loading new content. To address this,

if (request.readyState === 4 && request.status === 200){
                document.getElementById("signInBody").innerHTML = request.responseText;
                // Create a function for your form behaviour and call it here

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

I set up a website where every page had a consistent header using the HTML tag <head> <!--#include file="header.shtml"--> </head>, but out of nowhere, the Bootstrap functionality

I created a website with a common header that is included on all pages, but suddenly Bootstrap stopped working. What should I do now? Below is the code that was working fine previously but suddenly stopped yesterday: I have tried various solutions like r ...

How to eliminate the "br" tags automatically inserted after each paragraph in TinyMCE version 6

I have been trying to work with TinyMCE version 6 and I am struggling to prevent the addition of <br> after each paragraph. Whenever I insert a line of text and press enter, a new paragraph is created but there is always a <br> added between t ...

What is the best way to serve individual static files in Express without revealing the entire "/static" directory?

For my new project, I am working on a simple setup that involves using JWT for authorization. The project is being served entirely through express, with no separation between frontend and backend. My goal is to dynamically serve specific HTML files based o ...

show the stored value inside the useRef variable

One of my challenges involves a variable const prediction = useRef<any>(null); A button triggers a function that updates the variable's value: function showResult() { classifier.current.classify(capture, (result) => { ...

Adjusting color dynamically in three.js without real-time updates

I am currently working on developing a 3D configurator. https://i.sstatic.net/V4xWW.png My goal is to alter the color of a specific material using a color picker similar to this: $('#color-block').on('colorchange', function() { va ...

The visibility of MeshPhongMaterial is currently not displaying

As a beginner in the world of THREE.js, I am attempting to create a sphere that will serve as the foundation for a globe with texture. However, I have hit a roadblock while trying to implement MeshPhongMaterial, as nothing seems to appear on the screen. On ...

The ajax success response transforms when using @html.raw

In my Razor viewpage, I have the following jQuery code: $(document).ready(function () { var listValues = @Html.Raw(Json.Encode(Session["list"])); $("#nsline").click(function () { alert(listValues) $.ajax({ type: "P ...

Issue encountered while sending binary data to the server

After attempting to read a local file on the client side and sending it to the server, I encountered an error while trying to retrieve the image. JavaScript let req let rsp async function _post(url,data) { req = await fetch(url,{method: 'POST' ...

The functionality of Angular's ng-change or ng-click for select elements is not functioning properly

Exploring the concept of ng-change with checkboxes in this example. Check out the live example here Attempting to convert checkboxes into a selection but encountering some challenges. Here is the code snippet: <select> <option ng-model="confi ...

How to extract iframe page title with jQuery in JavaScript

Is there a way to retrieve the title of an iframe's source page and then update the title of the main page? ...

Error in Angular unit testing: Unable to access the 'subscribe' property as it is undefined

Currently, I am working on a starter kit template that includes authentication and an example with CRUD operations. You can find the code here: https://github.com/fransyozef/basic-login-angular Although I am new to unit testing, I am trying to set up test ...

Can you explain the inner workings of the service hierarchy within this Angular 2 application?

As a newcomer to Angular 2, I have a question regarding services. In the main view (associated with the app.component.ts class), I am faced with the following scenario: <div class="container"> <div class="row"> <div class="col-xs-12 ...

Combining multiple AngularJS expressions to form a URL within an interpolation statement

While this explanation may be lengthy, I appreciate your patience as I try to articulate the issue at hand. The error I'm currently encountering is as follows: Error: [$interpolate:noconcat] Error while interpolating: Strict Contextual Escaping disa ...

Unable to utilize angular-local-storage

Here is the code snippet I'm working with: angular.module('MyModule').controller('MyController', ['$scope', '$stateParams','$location', '$http','LocalStorageModule', function($s ...

What causes an AJAX POST request to fail?

While working on a simple HTML page with a form, I encountered an issue with my POST request failing without any response from the server. Can someone please help me figure out what I'm doing wrong? function createRequest(url, body) { var respons ...

Using Node.js to asynchronously loop through items and insert them into a MongoDB database

Currently, I am working on a project where I have a list of child collection Id's and running a for loop to find the parent collection data and insert it into another collection. However, I am facing an issue with the code execution as the for loop co ...

Modifying SVG gradients with JavaScript

My goal is to modify the stop color of a gradient displayed in an SVG element. I've been trying to adjust the stop-color attribute, but so far, my attempts have not been successful: <svg><defs> <linearGradient gradientTransform="rotat ...

Sending information to @select of multiselect in Vue.js - vue-multiselect

I'm currently working on passing a parameter to the @select event function in Vue.js HTML <template> <div class="container"> <div v-for="(billItem, k) in billItems" :key="k" > <div class=&q ...

Loop through an array to find the average values of different segments within the array

I am currently working with an array structured like this: ["2011-06-16 16:37:20",23.2], ["2011-06-21 16:37:20",35.6], ["2011-06-26 16:37:20",41.8], ["2011-07-01 16:37:20",25], ["2011-07-06 16:37:20",22.8], ["2011-07-11 16:37:20",36.4], ["2011-07-16 16:37 ...

Setting a specific time zone as the default in Flatpickr, rather than relying on the system's time zone, can be

Flatpickr relies on the Date object internally, which defaults to using the local time of the computer. I am currently using Flatpickr version 4.6.6 Is there a method to specify a specific time zone for flatpickr? ...