Solved

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

Posted on 2004-08-23
3
698 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

932 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

12 Experts available now in Live!

Get 1:1 Help Now