I have an HTML code:
<div id="first"></div>
I wish to use javascript (document.findElementById) to change the behavior of the div so that it'll be as:
<div id="first" align="center"></div>
How do I go about that?
I have an HTML code:
<div id="first"></div>
I wish to use javascript (document.findElementById) to change the behavior of the div so that it'll be as:
<div id="first" align="center"></div>
How do I go about that?
This works if you already have an align attribute for that element
document.getElementById("first").attributes['align'] = 'center';
If you dont already have the attribute, use setAttribute
document.getElementById('first').setAttribute('align', 'center');
document.getElementById('first').align = center
See How to add/update an attribute to an HTML element using JavaScript?
Unfortunately for you, the align
attribute is deprecated and shouldn't be used.
You can center DIVs of specific widths by setting the style margin
attributes, like so:
document.getElementById('first').style.marginLeft = 'auto';
document.getElementById('first').style.marginRight = 'auto';
If you're set on modifying the align
attribute, you can do it like so:
document.getElementById('first').align = 'center';
Other ways to center DIVs (using CSS) can be found here.
` in the div, that have margins themselves. I'm still working on a definitive solution.– Mr Lister Mar 01 '12 at 07:43
` is deprecated? Hey, you do know what `– Mr Lister Mar 01 '12 at 15:21` _means_, don't you?
` purely as a means of indenting is deprecated—see [here](http://www.w3.org/TR/html401/struct/text.html#edef-BLOCKQUOTE). If you're using it for actual block quotes you're fine. In general, with all of the backwards-compatibility of browsers, you're still pretty much assured that deprecated elements will work just fine. ;)– JacobEvelyn Mar 05 '12 at 18:45