css rules are not applied if the element is dynamically added using javascript

I'm encountering an issue with my code that inserts icons into a div with the ID of "editor". When I try to add a <select> element to the div with the ID of "drug_tool", the CSS styling rules for it are being ignored. How can I resolve this problem?

Here's the code I have written:

<div id="editor"></div>

<script>
var ni = document.getElementById('editor');
var newdiv = document.createElement('div');
var divIdName = 'glavni';
newdiv.setAttribute('id',divIdName);
ni.appendChild(newdiv);

var ni = document.getElementById('glavni');
var newdiv = document.createElement('div');
var divIdName = 'toolbar';
newdiv.setAttribute('id',divIdName);
ni.appendChild(newdiv);

var ni = document.getElementById('toolbar');
var newdiv = document.createElement('div');
var divIdName = 'prvi_tool';
newdiv.setAttribute('id',divIdName);
newdiv.innerHTML = 
        '<img src="images_zadaca/cut.png" onclick="" style="cursor:pointer;">   \
        <img src="images_zadaca/copy.png" onclick="removeElement(\'bold\')" style="cursor:pointer;">  \
        <img src="images_zadaca/paste.png" onclick="removeElement(\'bold\')" style="cursor:pointer;">  \
        <img src="images_zadaca/redo.png" onclick="izmijeni(\'redo\')" style="cursor:pointer;">  \
        <img src="images_zadaca/undo.png" onclick="izmijeni(\'undo\')" style="cursor:pointer;"> \
        <img src="images_zadaca/bold.png" onclick="izmijeni(\'bold\')" style="cursor:pointer;"> \
        <img src="images_zadaca/italic.png" onclick="izmijeni(\'italic\')" style="cursor:pointer;"> \
        <img src="images_zadaca/underline.png" onclick="izmijeni(\'underline\')" style="cursor:pointer;"> \
        <img src="images_zadaca/align_center.png" onclick="izmijeni(\'justifyCenter\')" style="cursor:pointer;"> \
        <img src="images_zadaca/align_justify.png" onclick="izmijeni(\'justifyFull\')" style="cursor:pointer;"> \
        <img src="images_zadaca/align_left.png" onclick="izmijeni(\'justifyLeft\')" style="cursor:pointer;"> \
        <img src="images_zadaca/align_right.png" onclick="izmijeni(\'justifyRight\')" style="cursor:pointer;">';
ni.appendChild(newdiv);

var ni = document.getElementById('toolbar');
var newdiv = document.createElement('div');
var divIdName = 'drugi_tool';
newdiv.setAttribute('id',divIdName);
newdiv.innerHTML = 
        '  \
    <select id="select2" onchange="bojaFonta()"> \
        <option value="red">Red</option> \
        <option value="blue">Blue</option> \
        <option value="green">Green</option> \
        <option value="black">Black</option> \
        <option value="white">White</option> \
    </select>  \
        <img src="images_zadaca/copy.png" onclick="removeElement(\'bold\')" style="cursor:pointer;">  \
        <img src="images_zadaca/paste.png" onclick="removeElement(\'bold\')" style="cursor:pointer;">  \
        <img src="images_zadaca/redo.png" onclick="izmijeni(\'redo\')" style="cursor:pointer;">  \
        <img src="images_zadaca/undo.png" onclick="izmijeni(\'undo\')" style="cursor:pointer;"> \
        <img src="images_zadaca/bold.png" onclick="izmijeni(\'bold\')" style="cursor:pointer;"> \
        <img src="images_zadaca/italic.png" onclick="izmijeni(\'italic\')" style="cursor:pointer;"> \
        <img src="images_zadaca/underline.png" onclick="izmijeni(\'underline\')" style="cursor:pointer;"> \
        <img src="images_zadaca/align_center.png" onclick="izmijeni(\'justifyCenter\')" style="cursor:pointer;"> \
        <img src="images_zadaca/align_justify.png" onclick="izmijeni(\'justifyFull\')" style="cursor:pointer;"> \
        <img src="images_zadaca/align_left.png" onclick="izmijeni(\'justifyLeft\')" style="cursor:pointer;"> \
        <img src="images_zadaca/align_right.png" onclick="izmijeni(\'justifyRight\')" style="cursor:pointer;">';
ni.appendChild(newdiv);


</script>

Answer №1

When it comes to styling the select element, you are limited to using only two properties: color and background-color.

If you want more information, you can check out MSDN's detailed explanation:

According to MSDN, apart from background-color and color, any style settings applied to the option element through the style object will be disregarded. Additionally, any style settings applied directly to individual options will take precedence over those applied to the entire select element.

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

The MongoDB operator "$in" does not recognize the type array that currently exists in the field

When attempting to execute an aggregate query in mongodb, I encountered an issue with the "$in" operator stating that the field passed is not recognized as an array. Here is the aggregate query being used: [ { '$match': { 'isVis ...

Show the results sequentially as the PHP script runs

In my current setup, I am working with Order IDs and their respective product requests. When a user selects multiple order IDs and clicks on the start process button, I compare the product requests against available stock levels. The final result displays ...

Creating an AJAX function to display a popup window for users who are already registered - here's how!

I am currently working on a dropwizard-java project. Whenever users click the subscribe button, it displays a thank you message for subscribing and then checks if the user is already registered. I would like to have a pop-up window immediately show whethe ...

Is there a way to fully break out of a .forEach loop that's nested within a function?

Here is the method I am currently working with: wordFormDirty = (): boolean => { var self = this; angular.forEach(self.word.wordForms, function (wf, key) { var wordFormNgForm = 'wordFormNgForm_' + wf.wordFormId if ...

Observing the state of a variable within a directive using a service from a different module

I currently have two modules named "core" and "ui". The "ui" module has a dependency on the "core" module. Below is the code for my core.js file: var core = angular.module('core', [ 'ngRoute' ]); // Services core.service('httpI ...

I am experiencing an issue with my jQuery loop code not functioning properly when using the .each method within the loop

I am struggling with the following code. <input type="text" name="1" class = "inp<?=$p?>"> <input type="text" name="2" class = "inp<?=$p?>"> <input type="text" name="3" class = "inp<?=$p?>"> <input type="text" na ...

Retrieve data from an external website containing an HTML table without a table ID using Java Script and transform it into JSON

I have developed a script that can convert HTML table data into a JSON Object. To accomplish this task, I utilized the jquery plugin created by lightswitch05. With this code, I am able to extract data from an HTML table on the same web page using: var t ...

Creating Dynamic Buttons in Laravel with AJAX and jQuery

Hello, I need help creating dynamic buttons as I am not familiar with ajax or jquery. I currently have a dynamic button running, but I want to create another dynamic button after I add another form. Here is my code. Please take a look at my image, as I am ...

Creating a JavaScript array filled with database data using PHP

Below is a snippet of PHP code used to establish a connection to a database: <?php $host = "localhost"; $user = "admin"; $password = ""; $dbname = "test"; $con = new mysqli($host, $user, $password, $dbname) or die ('Could not connect to the d ...

Using AJAX to Query a PHP Database

Currently, I am implementing an AJAX call from the YouTube player JavaScript to a PHP page that contains various functions, mainly involving database inserts. In my PHP code, I use a case statement to determine which function is being called based on the d ...

"Troubleshooting the failure of the alert function to work properly when loading content

I am working on a webpage named index.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Co ...

efficiency of this algorithm

function checkSubset(array1, array2) { const set1 = new Set(array1); const set2 = new Set(array2); for (const element of set2) { if (!set1.has(element)) { return false; } } return true; } Can we consid ...

The React JSON Unhandled Rejection problem requires immediate attention

While working on a form in React 16, I reached out to a tutor for some guidance. However, when trying to mock the componentDidMount, I encountered an error that has left me puzzled. The app still runs fine, but I am curious as to why this error is occurrin ...

A Guide on Modifying a Pseudo Element When Clicked

How can I create a click handler in jQuery to change the color of an 'eyebrow' element above a Heading element? The eyebrow element is set up with a pseudo element in my project, and I'm struggling to make it change color when the heading is ...

Require the header text to appear black on a white background and white on a dark background

As someone who is not experienced in coding, I have tried multiple methods without success. It seems like none of the solutions available are compatible with my Squarespace theme. Any assistance you can provide would be greatly appreciated! Here is a link ...

Can Tescafe be used to simulate a login process?

Scenario: Testing the features of an app built with Testcafe and Vue requires being logged in, as being logged out redirects to the login page. Roles have been utilized to streamline the login process, but is there a way to simulate logging in without actu ...

What causes bootstrap to fail on smaller screens?

While developing an app using Bootstrap, I encountered an issue where the app was not functioning properly on small screens. Everything worked perfectly on larger screens, such as a PC browser, but on a mobile browser, none of the tabs would open. When I t ...

Determine changes in data retrieved from a JSON file using React

I've been working on a cryptocurrency app using React and a JSON API to fetch the latest data. My approach involves using fetch to load the JSON API and setInterval to refresh the app every 10 seconds. Now, I'm wondering if there's a way to ...

Cookies store their values within the document.cookie framework

My cookie contains the following data: emailID=a1%40a.comSEPmaths=0SEPphysics=0SEPchemistry=0SEPbotany=0SEPzoology=0SEPta mil=0SEPenglish=0SEPpolity=0SEPgk=0SEPhistory=0 However, when I use document.cookie.split('; '), it returns the encoded ve ...

Send data containing special characters through a GET request in PHP

I am looking for a way to send any text as a GET parameter to a PHP script. Currently, I am simply appending the text like this: action.php?text=Hello+my+name+is+bob This URL is generated using JavaScript and then used in an AJAX request. In action.php, ...