www.webdeveloper.com
Results 1 to 6 of 6

Thread: Change element width using javascript

  1. #1
    Join Date
    Aug 2011
    Posts
    42

    Change element width using javascript

    Hi, I am simply trying to increase a divs width everytime a button is pressed but I can't figure out how or why. any help greatly appreciated:

    Code:
    <!DOCTYPE HTML>
    <html lang="en">
    <head>
    	<title>title</title>
    
    <style>
    .progressPercent {background:#ccc;height:20px;width:50px}
    </style>
    
    <script>
    		var increaseProgress = (function () {
    			var num = 100;
    			var elem = document.getElementsByClassName('progressPercent');
    			return function () {
    				num += 100;
    				elem.style.width = num + 'px';
    			}
    		})();
    	</script>
    	
    </head>
    <body>
    	<a href="#" onclick="increaseProgress(); return false;">BUTTON</a>
    	<div class="progressPercent">&nbsp;</div>
    </body>
    </html>

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    There are several ways. One is to grab the offset properties of the element
    Code:
    <script type="text/javascript">
    function increaseProgress(){
    var num=100;
    var div=document.getElementsByClassName('progressPercent')[0];
    var divWidth=div.offsetWidth;
    div.style.width=(divWidth+num)+'px';
    }
    </script>
    Note that the method document.getElementsByClassName() returns always a collection, not a single element, thus you must specify the index, [0] your case.
    Last edited by Kor; 09-26-2011 at 07:29 AM. Reason: typo. I have removed the equal sign

  3. #3
    Join Date
    Aug 2011
    Posts
    42
    thanks for your quick response but this doesn't work for me

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Typo: I have inserted an equal sign, by mistake. Test this:
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <style type="text/css">
    .progressPercent {background:#ccc;height:20px;width:50px}
    </style>
    <script type="text/javascript">
    function increaseProgress(){
    var num=100;
    var div=document.getElementsByClassName('progressPercent')[0];
    var divWidth=div.offsetWidth;
    div.style.width=(divWidth+num)+'px';
    }
    </script>
    </head>
    <body>
    <div>
    	<a href="#" onclick="increaseProgress(); return false;">BUTTON</a>
    	<div class="progressPercent">&nbsp;</div>
    </div>
    </body>
    </html>

  5. #5
    Join Date
    Aug 2011
    Posts
    42
    ok thank you. how could I use this to get all the elements on the page with that class?

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    use a loop:
    Code:
    function increaseProgress(){
    var num=100;
    var divs=document.getElementsByClassName('progressPercent'), i=0, d, divWidth;
    while(d=divs[i++]){
    divWidth=d.offsetWidth;
    d.style.width=(divWidth+num)+'px';
    }
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles