Unable to access and control HTML elements using JavaScript

let button = document.getElementsByTagName('button');
            button.value = 8;

            //button.textCont = 'X'; --- DOESN'T WORK.
            //button.innerHTML = 'X'; --- DOESN'T WORK.
body {
            background-color: navy;
        }

        h1 {
            font-family: 'monospace';
            text-align: center;
        }

        button {
            width: 80px;
            height: 80px;
            position: relative;
            left: 25px;
            top: 15px;
            background-color: darkblue;
            border: none;
            margin: 6px;
        }


        .game-box {
            background-color: black;
            width: 360px;
            height: 360px;
            border-radius: 12%;
            margin: auto;

        }

        .shave-left-top {
            border-top-left-radius: 12%;
        }

        .shave-right-top {
            border-top-right-radius: 12%;
        }

        .shave-left-bottom {
            border-bottom-left-radius: 12%;
        }

        .shave-right-bottom {
            border-bottom-right-radius: 12%;
        }
<!DOCTYPE html>
            <html>
            <head>
                <title>Tic Tac Toe Game</title>
                <link rel="stylesheet" type="text/css" href="style.css">
                <meta charset="utf-8">
            </head>
            <body class="container">
                <h1>Tic Tac Toe Game</h1>

                <div class="game-box">
                    <div class="row-1">
                        <button class="shave-left-top"></button> 
                        <button ></button> 
                        <button class="shave-right-top"></button> 
                    </div>

                    <div class="row-2">
                        <button></button> 
                        <button></button> 
                        <button></button> 
                    </div>

                    <div class="row-3">
                        <button class="shave-left-bottom"></button> 
                        <button></button> 
                        <button class="shave-right-bottom"></button> 
                    </div>

                </div>
                <script type="text/javascript" src="script.js"></script>
            </body>
            </html>

Hey everyone, struggling to change the button value with my JS code. Encountering an error in the console: SCRIPT7002: XMLHttpRequest: Network Error 0x80700013,

Downloaded the file but it doesn't seem to work, and the same error persists. Seeking help on this issue. Faced a similar problem while building a JS calculator. Posted about it but still no solution. Utilizing Sublime 3 and Edge browser. Appreciate any assistance. Thank you.

Answer №1

const btn = document.querySelectorAll('button');

querySelectorAll fetches all elements with the tag name button and returns a NodeList.

If you only have one button, you can target it directly by referencing the first element in the NodeList:

btn[0].value = 8;
   btn[0].textContent = 'X';
   btn[0].innerHTML = 'X';

Alternatively, you can loop through each button in the NodeList to modify their attributes:

for(let i = 0; i < btn.length; i++)
{
   btn[i].value = 8;
   btn[i].textContent = 'X';
   btn[i].innerHTML = 'X';
}

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

Retrieving Route Parameters in Angular.js from a URL

Is there a way for me to retrieve the value of access_token in my ng-controller as $routeParams from the URL below? http://www.example.com/#/access_token=asdfjaksjdhflanblasdfkjahdloahds ...

Concerns with the window's onload function

window.onload = prepareButton; function prepareButton() { document.getElementById('slist').onclick = function() { window.location = "students"; } } Whenever I click on a slist element, which is actually a <p> tag formatt ...

Stop the bootstrap accordion from expanding when a button in its header is clicked

Currently, I am facing an issue with two action buttons located in the header of an accordion. Here is the setup: https://i.sstatic.net/HU2Kp.png Whenever I click on one of these buttons, it toggles the accordion's state. I have attempted to use e.p ...

Replicate and modify the settings on a fresh radio inspection

In my approach, I am avoiding direct HTML editing and instead utilizing JavaScript/jQuery to accomplish the desired outcome. Initially, one input (specifically 'Express Shipping') is pre-selected by default. The goal is to clone/copy the HTML co ...

Guide to using the Strapi Upload Plugin: Uploading a file from a remote source using a scheduled task

Utilizing the strapi upload plugin with s3 as the provider has been a seamless experience when making API calls to the upload endpoint on my strapi instance (/upload). However, I am facing a dilemma with a cron job within our repository that monitors image ...

Getting information from a database using PHP and AngularJS through the $http.get method

Currently, I am utilizing an AngularJS script to retrieve data from an external PHP file that is encoded in JSON within an HTML page. The method $http.get(page2.php) has been employed to fetch a JSON-encoded array located in another file. However, the issu ...

Is it feasible to block indent the second line of a URL?

Is there a way to indent a second line of a URL so that it aligns perfectly with the first letter of the sentence? I attempted using <ul>, but encountered issues as it inherited small text and bullet points from the main .css file. Check out this li ...

What is the best way to divide two ranges that are intersecting?

Seeking a method to divide two overlapping ranges when they intersect. This is my current progress using typescript, type Range = { start: number; end: number; }; function splitOverlap(a: Range, b: Range): Range[][] { let result = []; const inters ...

Exploring the array push method in ES6 destructuring

Is it possible to use something similar to the destructing assignment feature in ES6 to write cleaner code when pushing items into an array? I'm unsure how to implement it properly, especially within a context like Vue.js. Here is an example code snip ...

Utilize JQuery to identify and select every parent element, then retrieve the height of the first child element and adjust the height of the

Recently, I developed a PHP script that pulls news and case posts from a database. The HTML structure used for displaying these posts is as follows: <a href='/post/placeholder'> <div class='col nopadding col12-12 counter'> ...

Issue with connecting JavaScript to HTML button

I am currently working on a 10-second countdown feature for a game using Javascript and HTML. Currently, the code runs upon page load. However, I am facing difficulty in linking it to a button so that it triggers upon clicking. ...

What are the best practices for setting access permissions when using Azure AD authorization flow?

I am in the process of creating a small Next.js application with the following structure: Authenticate a user via Azure AD using Next-Auth Allow the user to initiate a SQL Database Sync by clicking a button within the app with the access token obtained du ...

Encountering a TypeError while pre-rendering a page during the npm run build process

I am currently preloading this page on my Next.js application: const router = useRouter(); if (!router.isFallback && !postData?.slug) { return <p>Hmm... looks like an error</p> } const formatDate = date => { const newDa ...

How to request permissions from a bot user in Discord.js version 14?

I need to verify my bot's permissions before it carries out a command. Previously, everything was functioning perfectly with this code: // Discord.js v13 if (interaction.guild.me.permissions.has(Permissions.FLAGS.MANAGE_MESSAGES)) { interaction.re ...

Aligning columns next to one another using flexbox in a centered manner

I am attempting to create a layout with 3 columns, but I want any extra columns beyond a multiple of 3 to be centered instead of aligned left. While I have made progress, the issue I'm facing is that the additional columns are not centered relative t ...

Transforming all numbers to a consistent scale with the help of Numeral JS

Is there a way to customize the output of the numeral.js function so that it always returns numbers in thousands? For example, converting 1000000 to 1000k and 100 to 0.1k. console.log( numeral(1000000).format('0a') ); <script src="https ...

Encountering a "file or directory not found" error during the installation of ply using

I recently stumbled upon an interesting project for testing Rest APIs. I was in the process of installing ply from https://github.com/ply-ct/ply npm install ply-ct --save-dev Encountered this error, wondering if anyone has a solution npm WARN saveError EN ...

Should the text underneath the image be placed above the rest of the images?

In the process of developing my game, I encountered a challenge where text needs to be dragged under specific images. To achieve this, I envision creating tabs below the images for the text to be dragged onto. My initial approach involved wrapping each im ...

Tips for sending a div value that is not included in the model to the backend controller action

Question: I am looking to have a single div element in the user interface that can display a value added via JQuery and then be accessible in the backend once the form is submitted. View: @model ScheduleAptMV using (Html.BeginForm()) ...

Adjust the width of a div element automatically using CSS

I have a dropdown bar with various options that I want to display inline while ensuring they can scale to occupy the entire width of the div, allowing for multiple options per row. Below is a screenshot showing my current progress: https://i.sstatic.net/3 ...