Forum Moderators: open

Message Too Old, No Replies

dinamic drop down - problems in Opera

         

phoenix_fly

6:59 pm on May 18, 2005 (gmt 0)

10+ Year Member



Hello my friends,

I have this code, wich works fine in IE and Netscape, but not in Opera.

var field = window.parent.frames[1].document.form1.city;
field.options.length=0 ;

var group = new Array( "One", "Two", "Three" );

for (i=0;i<group.length;i++){

field.options[i]=new Option(group[i],group[i],false,false);

}

As you can see, Iīm updating the options of a drop-down menu. Opera doesnīt generate any complaint, what happens is that the options do get loaded, in the very number of elements there is in the array, but with nothing showing there, just blank options.

For me, this points that the problem is in this section:

new Option(group[i],group[i],false,false);

Any ideas of what can I do to please Opera?

Thanks a lot, folks

phoenix_fly

Bernard Marx

7:28 pm on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about going the DOM route instead? (createElement; appendChild etc)

recent: [webmasterworld.com...]
(post #13)

phoenix_fly

7:40 pm on May 18, 2005 (gmt 0)

10+ Year Member



Hey Bernard,

Sorry, but I am not familiar to DOM yet, so this would introduce a technology I do not master; not advised, right? (I plan to learn it soon, though)

Bernard Marx

9:18 pm on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's all the same thing, just different method names.
The whole procedure is laid out in the previously-referenced post.

"not advised?"
On the contrary. I'd do it now.

phoenix_fly

12:04 am on May 19, 2005 (gmt 0)

10+ Year Member



Sure, itīs a good thing to do, but you canīt imagine the thousand issues I still have to solve to have this site ready untill the deadline expires. I really have to make it work with code I already understand. Sorry.

Rambo Tribble

1:24 am on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a quick script demonstrating adding through the W3C DOM. You might find it is the only way to satisfy Opera and to insure your code has a future.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function addCat(select){
if (select.value!= "new") return;
var cat_name = prompt('Please enter category name:','');
if (!cat_name) return;
var new_option = document.createElement("option");
new_option.value = cat_name;
new_option.appendChild(document.createTextNode(cat_name));
select.insertBefore(new_option, select.lastChild);
select.selectedIndex = select.options.length-2;
}
</script>
</head>
<body>
<form name="test">
<select name="cats" onchange="addCat(this);">
<option value="general">General</option>
<option value="stuff">Stuff</option>
<option value="new">New category</option>
</select>
</form>
</body>
</html>

phoenix_fly

2:33 pm on May 20, 2005 (gmt 0)

10+ Year Member



Hello Marx and Rambo!

In fact, you guys are right, Iīve searched on this topic and it seems Opera development team is really making things easy for dear Bill. They simply do not provide support for this function. So, Iīll have to go DOM, or my code that will be DOoMed!

I tried to adapt the code Rambo sent me, but something isnīt right yet. Can you guys help me out?

The old code (without DOM) was:

function LoadMenu(){

var field= window.parent.frames[1].document.form1.city;

field.options.length=0 ; // deletes prior options list

var group = new Array( "Kansas", "New York", "Bagdad" );

for (i=0;i<group.length;i++){

field.options[i]=new Option(group[i],group[i],false,false);

}

}

And the new one (well, my attempt) is:

function LoadMenu(){

var field= window.parent.frames[1].document.form1.city;

field.options.length=0 ; // deletes prior options list

var group = new Array( "Kansas", "New York", "Bagdad" );

for (i=0;i<group.length;i++){

var new_option = document.createElement("option");
new_option.value = group[i];
new_option.appendChild(document.createTextNode(group[i]));
field.insertBefore(new_option, field.lastChild);
field.selectedIndex = field.options.length-2; // not sure what to do with this one...
}

}

I wait for your hand, then.

Thanks

phoenix_fly

Rambo Tribble

4:48 am on May 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't care what you say, I'm not going to marry you.

Try this:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function modCat(){
var sel_obj=document.getElementById('cats');
var s_o_ch_ln=sel_obj.childNodes.length-1;
for(var i=s_o_ch_ln;i>=0;i--){
sel_obj.removeChild(sel_obj.childNodes[i]);
}
var cat_name=['Siamese','Persian','alley']
var c_n_ln=cat_name.length;
for(var i=0;i<c_n_ln;i++){
var new_option = document.createElement("option");
new_option.value = cat_name[i];
new_option.appendChild(document.createTextNode(cat_name[i]));
sel_obj.appendChild(new_option);
}
}
</script>
</head>
<body>
<p>
<form name="test">
<select id="cats">
<option value="general">General</option>
<option value="stuff">Stuff</option>
<option value="new">New</option>
</select>
<a href="#" onclick="modCat();return false;">test</a>
</form>
</p>
</body>
</html>

phoenix_fly

2:55 pm on May 23, 2005 (gmt 0)

10+ Year Member



Hey, sorry for the english mistake. hahaha (By the way, how could I say "wait for a hand" saying that this expected help was from "you(r)"?)

Ok, I think I can do it, but need your help with only one more thing:

var sel_obj=document.getElementById('cats');

The select object is in another frame, remember?

var field= window.parent.frames[1].document.form1.city;

How can I reference it through the DOM syntax?

Thanks

phoenix_fly