I am experiencing an issue where the Ajax/Javascript functionality is unresponsive on my homepage

I have been attempting to design a button using a simple Ajax / Javascript command. It seems to be functioning properly, but I am encountering an issue where it does not appear on the main page when inspecting with F12, causing some complications.

Below is the code snippet:

<html>
    <head>
        <script src="js/jquery.js"></script>
    </head>
<body>
    <button class="createButton" id="create_Button" onclick='button_creation()'>Create me</button>
</body>

<script>
    function button_creation() 
    {
            button_exist = document.getElementById('submit_button');
            if(button_exist == null)
            {
                    btn = document.createElement("BUTTON");
                    btn.innerHTML = "Submit";
                    btn.setAttribute("type","submit");
                    btn.setAttribute("name","submit");
                    btn.setAttribute("value","submit");
                    document.body.appendChild(btn);
            }
    }
</script>

The script functions properly, but whenever I continue to press the button, it creates additional buttons. I believe this is because it does not appear on the F12 of the main page. However, when I access the console (F12) and type "document.getElementById('submit_button')", I can see all the values I entered.

Does anyone know why the Ajax / Javascript code is not appearing in my F12 and how I can resolve this issue?

Thank you in advance.

Answer №1

Pressing F12 will display the browser's debugging tool with the rendered source code. The appearance may differ slightly depending on whether you're using Chrome, Edge, Firefox, or other browsers as each has its own unique set of tools. However, this variation is not the root of the problem. Another thing to note is that HTML generated with JavaScript may not be immediately visible in the F12 code. In such cases, you might need to right-click and select 'inspect' on the new button to force a refresh (this will depend on the browser you're using).

Your current code is checking for a button with an ID of 'submit_button'. If it doesn't find one, it creates a new button. The issue arises when the new button is not assigned an ID value at all. Consequently, when the code checks again for buttons with the ID of 'submit_button', it will never locate any, causing it to repeatedly create new buttons. To rectify this, ensure that the newly created button is assigned an ID:

function createButton() {
    let existingButton = document.getElementById('submit_button');
    if (existingButton == null) {
        let btn = document.createElement("BUTTON");
        btn.innerHTML = "Submit";
        btn.setAttribute("id", "submit_button");
        btn.setAttribute("type","submit");
        btn.setAttribute("name","submit_button");
        btn.setAttribute("value","submit");
        document.body.appendChild(btn);
    }
}

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

Difficulty in accessing JSON data with Node.js

Encountering difficulties with retrieving JSON data from a JSON file, I am faced with the following error message: "NetworkError: 404 Not Found - http://localhost:8000/channels.json" Below is the code snippet used to fetch JSON data in my HTML file: ...

Guide for retrieving the maximum length of an input field using JavaScript

Is it possible to retrieve the maxlength of an input field using JavaScript? <input type="password" id="password" maxlength="20" > I attempted to do this, however it only returns undefined console.log(document.getElementById("password").maxlength) ...

Dealing with special characters in Mustache.js: handling the forward slash

I have a JSON file that contains information about a product. Here is an example of the data: { "products": [ { "title": "United Colors of Benetton Men's Shirt", "description": "Cool, breezy and charming – this s ...

Ways to customize the hover effect for react-bootstrap navbar link

I'm looking to update the hover effect color from white to green, but I'm having trouble finding the correct CSS code for it. <Navbar fixed='top' collapseOnSelect expand="md" variant="dark" className="animate ...

Using Leaflet JS to implement multiple waypoints on a map

I am trying to create a route with multiple waypoints without hardcoding any data. My waypoints array should dynamically include latitude and longitude values. How can I achieve this? var data = [ { "title": 'Chennai', " ...

I'm having trouble with AngularJS routes not functioning properly when accessed directly. I am using html5 mode in conjunction with

When accessing URLs directly in my Angular app either through the address bar or from external links, all routes default back to the home page. However, when navigating within the app, routes work as expected. I have come across similar issues on Stack Ov ...

Using JavaScript to set attribute values in Python Selenium, these values are cleared after each update

Assuming : for i in list('{}'.format(value)): self.browser.execute_script( "arguments[0].setAttribute('value', '{}');".format(i.replace('&b ...

Unraveling the Mystery of jQuery AJAX Response: Where Have I Gone Astray?

$.ajax({ type: 'POST', url: 'place/add', data: { lat: 45.6789, lng: -123.4567, name: "New Place", address: "123 Main St", phone: "555-1234", review: "Great place!", cat ...

Integrating Amazon external images in NextJS

There is a specific issue with loading images from a URL stored on Amazon S3 within the domain configured in next.config.js. Strangely, when using external URLs like Unsplash, the images load fine. The problematic URL is: idinheiro-admin-images.s3.sa-east ...

What steps should be taken to retrieve the contents of a file that has been chosen using the browse

You have successfully implemented a browse button that allows the user to navigate the directory and choose a file. The path and file name are then displayed in the text element of the Browse button complex. Now, the question arises - how can I extract dat ...

JavaScript image search function within the existing page

Building a HTML website is new to me and I lack basic programming skills. I've uploaded 100 animated images to another server and linked them to my HTML website using code like this: <p> <img src="http://.../abc/23118465.gif" alt="teddy be ...

What could be causing the issue with Vite build and npm serve not functioning together?

After shifting from CRA to VITE, I am encountering a problem with serving my app. I successfully build my app using vite build. and can serve it using Vite serve without any issues. However, I want to use npm's serve command. Whenever I run vite bui ...

Utilizing NPM packages within Grunt-powered workflows

When working with Node, the module loading system is quite straightforward using the require() method to load modules from various locations in the root folder. For example: var qr = require('qr-image'); However, I have been attempting to achi ...

Increase the current time by 50 minutes

I have a dropdown menu with various time options like this: <select name="" id="delyvery-hour"> <option value="18:00">18:00</option> <option value="18:05">18:05</option> <option ...

Creating, customizing, and assigning values to data attributes in Draft-js blocks: A complete guide

My current setup involves a DraftJS editor displayed like this: <Editor editorState={this.state.editorState} handleKeyCommand={this.handleKeyCommand} onChange={this.onChange} placeholder="Write a tweet..." ref="editor" spellCheck={true} /&g ...

Adding quotes is optional when using the append function

We are retrieving product options from the displayed html using variables PRODUCT_CUSTOM_1_ and PRODUCT_NAME_. Some products have quotations like " and '. However, when we post these strings elsewhere, they get cut off at the ". We need to either remo ...

The Ionic framework has a defined variable

In my code, I have initialized a variable inside the constructor like this: constructor(public http: HttpClient) { this.data = null; this.http.get(this.url).subscribe((datas: any) => { this.dbUrl = datas[0].db_url2; console.log(this ...

Top technique for mirroring a website

Recently, I created a directory for a client's website. The site is fully operational, but the client would like a replicated version for testing and learning purposes. To create a full replica of his website, it will require hours of work to change ...

Customizing jQuery's $.ajax() function to process data received from a PHP file using $_POST variables

Before I ask my question, here is the updated version of my HTML code: <?php $conn = //successfully connected to the database. $sql = "SELECT t1.column1 AS Column_1 FROM table1 t1"; $rs = mysqli_query($conn,$sql); $rows= mysqli_fetc ...

Ensure that any modifications made to an Angular service are reflected across all components that rely on that service

I am currently in the process of replicating a platform known as Kualitee.com, which serves as a test-management tool utilized by QA Engineers. Within Kualitee, users can access multiple projects, each containing various test cases and team members. The ab ...