Solved

How to resize the width of a select box at run time ?

Posted on 2004-08-23
3
697 Views
Last Modified: 2012-06-27
Hi,

    How can i control the width of a dropdown (Select box) at run time. I want to increase/decrese width of a select box from a javascript function. When i try to get hold of the style of the element and try to set  the width its giving an error that the attribute is ready only. Any solutions ??

thanks,
Hyder
0
Comment
Question by:hyder
3 Comments
 
LVL 5

Accepted Solution

by:
LindzK earned 250 total points
Comment Utility
Below is a javascript example to change the width of any object ( in this case a select box )  and some html for you to test it

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

<head>
  <title></title>
 
  <script language="javascript">
    // will resize a given object
      // variables
      // width = new width
      // id    = id of object
    function ReSize(width,id)
      {
        var selobj = document.getElementById(id);
        selobj.style.width = width;
      }
  </script>
 
</head>
<body>

<select id="SelectBox" name="SelectBox">
  <option value="aaa">aaa</option>
  <option selected value="bbb">bbb</option>
</select>

<input type="text" id="txtWidth" name="txtWidth" value="100px">
<input type="button" id="btnChangeSize" value="Change Size" onClick="ReSize(document.getElementById('txtWidth').value,'SelectBox')">

</body>
</html>
0
 
LVL 3

Assisted Solution

by:Ayesha_K
Ayesha_K earned 250 total points
Comment Utility
Hi,

Run the following code amd you'll get the idea how to change the width on runtime

<html>

      <head>
      </head>
      <body>
            <form method=post name=FR>
            Names: <select name="FRNames" size=1 style="Width:200px;"

onchange="this.style.width='300px'">
                  <option value="A">A Value</option>
                  <option value="B">B Value</option>
                  <option value="C">C Value</option>
                  <option value="D">D Value</option>
                  <option value="E">E Value</option>
                  <option value="F">F Value</option>
                  </select>
            </form>
      </body>
</html>

Regards
0
 
LVL 8

Expert Comment

by:_aaron_
Comment Utility
@hyder
Is this because ur trying to set several elements on the page to the width of the largest element? (ie make all the select elements the same width? ) If so, I will post a quick script to do this onload (ie page init)....
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now