Manipulating DOM Elements with the Style Attribute in JavaScript

I am faced with two different blocks of code:

var holder = document.createElement('div');

var a = document.createElement('div');
a.style.float = 'left';
a.style.width = '90px';
a.style.height = '90%';
a.style.border = '1px dotted black';

var b = document.createElement('div');
b.style.float = 'left';
b.style.width = '90px';
b.style.height = '90%';
b.style.border = '1px dotted black';

holder.appendChild(a);
holder.appendChild(b);

Then, there's another set of code:

var holder = document.createElement('div');

var a = document.createElement('div');
a.setAttribute('style', 'float:left; width:90px; height: 90%; border: 1px dotted black;');

var b = document.createElement('div');
b.setAttribute('style', 'float:left; width:250px; height: 90%; border: 1px dotted black;');

holder.appendChild(a);
holder.appendChild(b);

The first piece of code fails to display correctly as 'b' ends up below 'a'.

In contrast, the second block works perfectly well as it positions 'b' to the right of 'a'.

What could be causing this discrepancy?

Answer №1

There is a mistake here:

b.style.float ...

The correct syntax is:

b.style.cssFloat ... // non-ie
b.style.styleFloat ... // ie

You can safely set both properties for compatibility across all browsers without affecting the functionality of each other.

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 global class variable encounters an error when trying to assign the value of "socket" to it

Within my class constructor, I am setting up a Socket connection and then storing the socket parameter in a global class variable (this.socket_variable = socket) so that I can access it across all functions in the class. CODE const { Server } = require(&q ...

Ways to modify the background color of the entire body excluding a sidebar div

How do I implement an overlay and dim the screen after clicking on a menu button? <ul id="gn-menu" class="gn-menu-main"> <li class="gn-trigger"> <a class="gn-icon gn-icon-menu"><span>Menu</spa ...

Escaping double quotes in dynamic string content in Javascript can prevent unexpected identifier errors

Need help with binding the login user's name from a portal to a JavaScript variable. The user's name sometimes contains either single or double quotes. I am facing an issue where my JavaScript code is unable to read strings with double quotes. ...

Failure of html and javascript banner in Adobe AIR application

Here's a simple breakdown of the Adobe AIR application setup. The main window of the application is an HTML file, and the `application.xml` file looks like this: <initialWindow> <title>window title</title> <content>i ...

Safari fails to refresh CSS when it comes to dynamic attributes

Take a look at this simple demonstration: http://jsfiddle.net/fE8ry/ I am attempting to modify the attribute of a div, and based on the attribute's value, apply the corresponding CSS. When the page loads, the attribute selector functions correctly a ...

The button is converting my text to a string instead of the integer format that I require

Hello everyone, I've been grappling with this button conundrum for the last 45 minutes, and I can't seem to find a solution. I have created three different buttons in my code snippet below. (html) <div class="action"> ...

The Javascript eval method throws a ReferenceError when the variable is not defined

In my constructor, I was trying to create a dynamic variable from a string. Everything was working smoothly until I suddenly encountered this error out of nowhere. I didn't make any changes that could potentially disrupt the system, and the variables ...

Utilizing the v-for directive to loop through JSON data with unique IDs and linking them to Input components in PrimeVue

In my database, I have a collection of products with a column named attributes that stores property/value pairs in JSON format. Each product can have unique attributes. For instance, one product's attributes could be: #product1 attributes { color: & ...

Enhance Animation Fluidity with HTML/CSS

I am experimenting with creating a floating animation for my iceberg logo by adjusting the top margin. Below is the CSS code I have implemented: img { height: 60px; padding: 5px; -webkit-animation: logofloat 1s ease-in-out infinite alternate; -moz ...

Mistakes in design when transforming inline SVG to PNG format

One of my main objectives is to convert a <div> element that contains multiple inline svg images into a png file. The entire process must be carried out within the client's browser using JavaScript. I have experimented with various techniques: ...

Determining the button's width relative to its parent's size using a

I'm trying to make my button take up 90% of the width of the screen. After testing my HTML code, I noticed that the button is only occupying around 10% of the screen. However, when I specify the width using pixels, it adjusts correctly. What could b ...

There was an unexpected error in angular.js while trying to work on a calendar widget based on angular-ui-calendar. The error message indicated that the argument 'fn' was expected to be a function but instead received Moment

I came across a similar topic earlier on Stack Overflow, but unfortunately, the issue wasn't resolved. So, I've decided to revisit this question and address it myself this time. In my app.js file, which is where my main module for the app is ini ...

What is the process for determining the overall cost of a column in Angular 9?

I am facing a challenge in calculating the total price, total quantity, and total counted(#) of each column in a table. | Item | Price | Quantity | 1 | Mouse | 500 | 1 | 2 | Wire | 100 | 2 | TI:3 | |TP:600 | TQ:3 | < ...

"Navigate back to a previous page in Vue Router without having to

I am currently exploring the option of creating a back button in my Vue.js application using vue-router that mimics the behavior of the browser's native back button. The challenge I'm facing is that when using history mode for the router and tryi ...

Is there a way I can maintain the active state after clicking on a link?

Is there a way to maintain an active element state when navigating to another page after clicking a link? I am wondering if it is possible to use JavaScript to remove the active state from one link and apply it to the next one. I am facing some challenges ...

What strategy is most effective for implementing an isAuthor guard in nestjs?

Currently, I've been enrolled in online courses on Udemy and experimenting with the use of the guard middleware. In addition to creating the admin.guard and auth.guard as suggested by tutorials, I'm considering implementing an isAuthor.guard whe ...

Assistance with designing in JavaScript and Python

Currently, I have a setup where my external website is extracting data from an iframe within our internal company intranet using Javascript. The extraction process is successful, but now I am faced with the challenge of accessing this harvested data in ord ...

Getting the Height and Width of an Image during the upload process in a React application

Can anyone help me with retrieving the Image Height and Width during image upload? I have attempted the following code, but it always gives me 0 0: const uploadedImage = e.target.files[0]; var image = new Image(); image.src = uploadedImage; console.log( ...

What could be causing my recursive function to not display the intended outcome?

In my search method, I traverse a nested object recursively to find a specific folder name: findSpecifiedFolder(data) { const bookmarks = JSON.parse(data).roots.bookmark_bar.children; const search = bookmarks => { for(let folder of book ...

There is no index signature that accepts a parameter of type 'string' in the type '{ [key: string]: AbstractControl; }'

I'm currently tackling a challenge in my Angular project where I am creating a custom validator for a reactive form. However, I've encountered an error within the custom validators function that I am constructing. Below you will find the relevan ...