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
Solved

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

808 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