views:

64

answers:

2

I have a situation where I have several listbox controls on the same asp.net page. They are currently autosized to keep data from truncating. However, I would like to be able to determine the width of the largest listbox and then alter the size of the other listboxes to be the same.

The question is... How can I access the size of the listboxes? Or Can I?

A: 

Yes you can...

//This is a <div> that wraps around your listboxes
var wrapperDiv = document.getElementById('listboxWrapper');

//Get all <select> elements within the <div>
var sels = wrapperDiv.getElementsByTagName('SELECT');

//An array to store the width values
var widths = new Array();

//Load the array
for(var i = 0, l = sels.length; i < l; i++)
  widths[i] = sels[i].offsetWidth;

//Get the max width
var maxW = Math.max(widths);

//Set the max width to all the list boxes
for(var sel in sels)
  sels[sel].style.width = maxW;
Josh Stodola
too much comment use -> //Get the max width \n var maxW = Math.max(widths);
ck
I know, but I want all the Javascript newbies to understand.
Josh Stodola
Should say this is JavaScript, so run client side, /not/ on the server.
Richard
@Richard: His question is tagged "Javascript"
Josh Stodola
It was just what I was looking for. Thanks!
D.
A: 

for the best results, consider using a javascript framework, using jquery:

var width = 0;
//get the largest width
$("select").each(function() {
    if ($(this).width() > width) {
        width = $(this).width();
     }
});
//make them all the same width
$("select").css("width", width);
Jeremy B.
A simple task shouldn't require a big library.
Josh Stodola
personal preference aside, you don't know the scope of his project, anything over 1 page should use a javascript framework and, seeing as anyone who has used google already has the library in cache your point is moot
Jeremy B.
Jeremy, your approach to Javascript is not as smart as you think it is. Read: http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3d90e8b8a8301462
Josh Stodola
clearly you dislike jquery, I'm not going to bother coming back again to argue the fact you were able to find someone on the internet speaking ill of it, thats less than an accomplishment. Thank you for being snide, it proves a lot.
Jeremy B.
Wow, you are ignorant. I absolutely *love* jquery. However, the asker doesn't mention it, and he/she clearly does not need it. And for what it's worth, the guy complaining about jQuery on the link is a very credible Javascript genius.
Josh Stodola
I had hope SO had a higher quality of user, I am disappointed. you are correct he did not ask about jquery, that being true, your tone, attitude and demeanor are something best left to immature web boards. Only the insecure need to cut down others to prove their own worth. Lets leave this now.
Jeremy B.
High quality of user? And what do you consider yourself after moaning and growning over a down-vote? What's the matter - any difference of opinion rubs you the wrong way? If you cant handle the community-based system here, then go be a troll elsewhere. Good day.
Josh Stodola
No moaning or groaning, didn't even mention the down vote, "not as smart..." "ignorant", "troll", resorting to personal attacks, and finding the need to act in your general fashion is immature at best. If you have any defense feel free to email me, any further comments are merely combative.
Jeremy B.
You know you mentioned the down-vote, and you deleted it after I answered. Then you decided to freak out and insist that any multi-page site should be using a relatively costly library. I should have never answered your original comment (duly noted, for future reference). Bye now.
Josh Stodola