Pulldown-menu created with JavaScript/DOM; Wrong width in IE

Joined
Feb 4, 2010
Messages
28
Reaction score
0
Hi,

I am trying to generate a pulldown-menu with JavaScript/DOM:
The following Code works fine with Opera an Mozilla but in the IE the
width of the select element is too short:

myCurrentElement =
window.document.getElementsByName('par_role')[0];
for (var i = 0; i < optionArray.length; i++)
{
myNewElement =
window.document.createElement('option');
myNewElement.setAttribute('value',
optionArray["value"]);
if (optionArray["selected"]==1)
{
myNewElement.setAttribute('selected',
'selected');
}
myNewText =
window.document.createTextNode(optionArray["label"]);
myNewElement.appendChild(myNewText);
myCurrentElement.appendChild(myNewElement);
}

<select size="1" name="par_role"">
<script language="JavaScript">
<!-- //
var j = optionArray.length;
optionArray[j] = new Object();
optionArray[j]["value"] = "1";
optionArray[j]["label"] = "Admin";

var j = optionArray.length;
optionArray[j] = new Object();
optionArray[j]["value"] = "4";
optionArray[j]["selected"] = "1";
optionArray[j]["label"] = "TEST";
// -->
</script>

If I try to set the width of the select-element with css, long labels
are cut off.
Do I have any other possibility to reset the width of the select
element?

Regards
 
Last edited by a moderator:
I

ivan.pavlov

> If I try to set the width of the select-element with css, long labels
are cut off.
Do I have any other possibility to reset the width of the select
element?

Regards

Styling select boxes with css is almost impossible because different
browsers have different rendering. I advice you to try this approach -
http://icant.co.uk/forreview/tamingselect/
 
Last edited by a moderator:
R

Richard Cornford

> I am trying to generate a pulldown-menu with
JavaScript/DOM: The following Code works fine with
Opera an Mozilla but in the IE the width of the select
element is too short:

myCurrentElement =
window.document.getElementsByName('par_role')[0];
for (var i = 0; i < optionArray.length; i++)
> optionArray[j]["label"] = "TEST";
// -->
</script>

If I try to set the width of the select-element with css,
long labels are cut off.
Do I have any other possibility to reset the width of
the select element?

You are suffering form all that needless complexity in your code.
Abandon DOM for this and do it the traditional way. It will work
reliably and the elements will be the desired size without styling:-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
</head>
<body onload="fillSelect();">

<script type="text/javascript">
function fillSelect(){
var sel, el, op = optionArray;
if((window.Option)&&(document.getElementsByName)){
if((el = document.getElementsByName('par_role')[0])){
el.options.length = 0;
for(var c = 0;c < optionArray.length;++c){
sel = Boolean(op[c].selected);
el.options[c] = new Option(op[c].label, op[c].value, sel, sel);
}
}
}
}
</script>
<script type="text/javascript">
var optionArray = [
{
value:1,
label:"Admin"
},
{
value:4,
selected:1,
label:"TEST"
}
];
</script>

<select size="1" name="par_role"><option>Non-viable web page</select>
</body>
</html>

Richard.
 
Last edited by a moderator:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top