Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline style edited with JavaScript is being ignored

I'm trying to use document.getElementByClassId().style.color to change the color of an element in my HTML document. I can see that it does add an inline style tag to the element but they are seemingly ignored by the browser.

I tried using the !important tag but that changed nothing. Here is my HTML document. I changed my CSS for the elements I want to modify into inline tags but they are ignored as well. Everything else works so far.

Here is my code (I'm a beginner please go easy on me :/).

//Only gets used to make the rows array.
var cells = [];

//Array that contains the entirety of the table element.
var rows = [];

//Random number to be used to color a random cell.
var rand = 0;


//Unique ID number that gets applied to each cell on the gameboard.
var id = 0;

//Calls cellMake.
var makeBoard = function(size) {
    cellMake(size);
}

//Adds opening and closing tags at the beginning and end of each string then writes them to the rows array.
var rowMake = function(num) {
    for(c = 0; num > c; c++) {
        rows[c] = '<tr>' + cells[c] + '</tr>';
    }
    writeBoard();
}

//Writes cell elements to the cells array based on how many columns the makeBoard function was called for.
var cellMake = function(num) {
    for(a = 0; num > a; a++) {
        cells[a] = '';
        for(b = 0; num > b; b++) {
            cells[a] += '<td id="' + id + '" class="pixel">';
            id++;
        }
    }
    rowMake(num);
}

//Chooses random pixel from the board and sets its color.
var choosePixel = function() {
    rand = Math.round(Math.random() * rows.length * rows.length);
    console.log(rand);
    document.getElementById(rand).style.color = 'red';
}

//Writes each element of the rows array onto the HTML document.
var writeBoard = function() {
    for(d = 0; rows.length > d; d++) {
        document.getElementById('gameboard').innerHTML += rows[d];
    }
    choosePixel();
}

window.onload = function() {
    makeBoard(50);
}
<!DOCTYPE html>
<html>
    <head>
        <title>Can I write JS?</title>
        <script src="script.js"></script>
        <style>
            body {
                text-align: center;
                background-color: black;
            }
            #gameboard {
                display: inline-block;
                margin: 0 auto;
                padding-top: 5%;
            }
            .pixel {
                height: 5px;
                width: 5px;
            }
        </style>
    </head>
    <body>
        <table id="gameboard"></table>
    </body>
</html>
like image 615
C1RRU5 Avatar asked May 10 '26 15:05

C1RRU5


1 Answers

You have to style the backgroundColor instead of color, because the cells don't contain any text, to which the color would be applied to

//Only gets used to make the rows array.
var cells = [];

//Array that contains the entirety of the table element.
var rows = [];

//Random number to be used to color a random cell.
var rand = 0;


//Unique ID number that gets applied to each cell on the gameboard.
var id = 0;

//Calls cellMake.
var makeBoard = function(size) {
    cellMake(size);
}

//Adds opening and closing tags at the beginning and end of each string then writes them to the rows array.
var rowMake = function(num) {
    for(c = 0; num > c; c++) {
        rows[c] = '<tr>' + cells[c] + '</tr>';
    }
    writeBoard();
}

//Writes cell elements to the cells array based on how many columns the makeBoard function was called for.
var cellMake = function(num) {
    for(a = 0; num > a; a++) {
        cells[a] = '';
        for(b = 0; num > b; b++) {
            cells[a] += '<td id="' + id + '" class="pixel"></td>';
            id++;
        }
    }
    rowMake(num);
}

//Chooses random pixel from the board and sets its color.
var choosePixel = function() {
    rand = Math.round(Math.random() * rows.length * rows.length);
    console.log(rand);
    document.getElementById(rand).style.backgroundColor = 'red';
}

//Writes each element of the rows array onto the HTML document.
var writeBoard = function() {
    for(d = 0; rows.length > d; d++) {
        document.getElementById('gameboard').innerHTML += rows[d];
    }
    choosePixel();
}

window.onload = function() {
    makeBoard(50);
}
<!DOCTYPE html>
<html>
    <head>
        <title>Can I write JS?</title>
        <script src="script.js"></script>
        <style>
            body {
                text-align: center;
                background-color: black;
            }
            #gameboard {
                display: inline-block;
                margin: 0 auto;
                padding-top: 5%;
            }
            .pixel {
                height: 5px;
                width: 5px;
            }
        </style>
    </head>
    <body>
        <table id="gameboard"></table>
    </body>
</html>
like image 130
Sebastian Speitel Avatar answered May 12 '26 04:05

Sebastian Speitel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!