Skip to content
Advertisement

Jquery append div and dynamically set the width

I’m trying to append a div to a td in a table. However, as I am appending the div, I want to first take the clientWidth of the td (to take into account different screensize) and then set the width of the div to be that width:

JavaScript

Now I’m trying to apply the 165 value to Div1 (where the width is currently hardcoded to 150px – I’d want to not hardocode it but dynamically set it), but I have no clue how to do this, I tried to do things like:

JavaScript

and

JavaScript

Ideally I would want to set the width as I append it so that when the text id displayed it is displayed in the correct width.

Thanks in advance.

Advertisement

Answer

There is no HTML attribute called Div1 or msg. Hence, this becomes invalid HTML.

You can append the width like this without hardcoding it initially.

JavaScript

Or else, you can assign an id and later change the width based on id.

JavaScript

The change the width using the id.

JavaScript

Use the first method if you are not changing the div width dynamically later in the program.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement