Solved

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

Posted on 2004-08-23
3
702 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Accepted Solution

by:
LindzK earned 250 total points
ID: 11871027
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
ID: 11871348
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_
ID: 11877745
@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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

729 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