Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the width of an element using Javascript?

How do I change width value assigned in #center? (I don't want to switch between CSS files just because 1 changed width value.) How do I dynamically change the value using Javascript?

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style type="text/css">
    #main #center {
        width: 600px; // change width to 700 on click
        float: left;
    }

    #main #center .send {
        background: #fff;
        padding: 10px;
        position: relative;
    }

    #main #center .send .music {
        text-align: center;
        margin-bottom: 10px;
    }

    #main #center .send .write {
        font-family: georgia, serif;
        font-size: 150px;
        opacity: 0.2;
    }

    //....etc.
</style>
</head>

<body>
    // change #center width to 700
    <a href="#go2" onclick="how to change width of #center ???" ></a> 
</body>
</html>
like image 264
JarM Avatar asked Oct 27 '25 00:10

JarM


2 Answers

document.getElementById('center').style.width = "599px";
like image 153
Blazemonger Avatar answered Oct 29 '25 16:10

Blazemonger


Two possible ways:

  1. Give the element "center" a class value that matches another CSS rule
  2. Alter the "style" property of the element
like image 26
Pointy Avatar answered Oct 29 '25 15:10

Pointy



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!