473,395 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Add value to 'style.width' attribute

bugboy
160 100+
Hi, I'm trying to add a number to the width of an element but the width value doesn't seem to be numeric even after i remove the "px" from it.

How can i add to an existing width? Below is my stab at it but i get 200200px instead of 400px.

Thanks!

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. echo'<div id="element"  style="width:200px;">';
  3. $w = 200;
  4. echo'<script type="text/javascript"> 
  5.     var w = document.getElementById("element").style.width;
  6.     w = w.replace(/px/, "");
  7.     w = w + '.$w.'+ "px";
  8.     document.getElementById("element").style.width = w;
  9. </script>';
  10. ?>
Feb 11 '09 #1
4 12096
acoder
16,027 Expert Mod 8TB
Use parseInt() to get the integer value.

In this case, why not use PHP to set the width directly instead of using JavaScript?
Feb 11 '09 #2
bugboy
160 100+
Thanks, That's what i'm looking for!

I'm changing the width of a container that holds several divs arranged like colunms. I'm adding additional div's/columns to the container with ajax while trying to keep them horizontally aligned, so, the container needs to expand to fit them in otherwise they will be offset vertically.

Here's my thinking.. If i were to update the entire container from the server then i would have to also update all of it's contents which could be 300kb+. By just adding the new content and then adjusting the container client side it saves resending the data for the existing content from the server every time i add a new div to the container. I suppose i could hold the content client side and re render it.. but it seems simpler to just adjust the container width with js.

Am i right to do it this way?

Thanks again.

bugboy
Feb 11 '09 #3
acoder
16,027 Expert Mod 8TB
I based my observation on the code you posted:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. echo'<div id="element"  style="width:200px;">';
  3. $w = 200;
  4. echo'<script type="text/javascript"> 
  5.     var w = document.getElementById("element").style.width;
  6.     w = w.replace(/px/, "");
  7.     w = w + '.$w.'+ "px";
  8.     document.getElementById("element").style.width = w;
  9. </script>';
  10. ?>
which could simply be replaced with:
Expand|Select|Wrap|Line Numbers
  1. echo '<div id="element"  style="width:400px;">';
Feb 12 '09 #4
hsriat
1,654 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. echo'<div id="element"  style="width:', (200 + $w) , 'px;">';
Feb 14 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Alexander | last post by:
Please, excuse me if the question is a basic one but I have not found any hint neither on XML4C samples, nor on Apache site. I'm using XML4C. I'm parsing an xml file and I'm getting to an attribute...
8
by: BiNZGi | last post by:
Hi I have reduced the problem to this code: <form> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td><input type="text" style="width: 100%;" value="Lorem ipsum dolor...
15
by: Gérard Talbot | last post by:
Hello all, I'd like to know and understand the difference between, say, <img src="/ImageFilename.png" width="123" height="456" alt=""> and <img src="/ImageFilename.png" style="width:...
0
by: RYoung | last post by:
I have a custom attribute with an "IsPresent" property with get; set accessors. When the attribute is applied to a field of a class, IsPresent will be false by default. At runtime, I'll read in...
3
by: The Developer | last post by:
Hi All, I have a web application where I am adding a custom attribute to my ASP.NET text box control and changing value of that attribute at client side using JavaScript. My problem is that...
4
by: Dabbler | last post by:
I'm trying to block out some text by setting Label width attribute which renders as a span tag so that all labels for textfields take up the same amount of space hence the textfields are all...
3
by: jdlwright | last post by:
Hi, I need to place a DIV exactly over a textarea element, but I can't calculate the correct value that I should set style.width to, because offsetWidth and style.width are calculated differently...
4
by: Paul Hadfield | last post by:
Hi, Wonder if anyone can help me on this, In DotNet2.0 I've been quite happily using the WriteAttributeString method of XmlWriter object, but have run into a problem when trying to output a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.