/    Sign up×
Community /Pin to ProfileBookmark

dynamically created select tag and option bug in ie6 (selected index)

I am really going crazy because of this. I have a javascript code that creates select instances and adds them to a form just before it is send to server. I don’t even need to display them. But ie6 always sets the selected index of this dynamically created select to 0. So the first option is send to server as selected. This messes things up in the server. How on earth can I create a select and add options in ie6 without selecting them. This is supposed to be a listbox with no selected items. No option should be marked as selected unless I want. I’ve spent hours and could not find an answer. Your help would be much appreciated

Kind regards
Seref

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@Sterling_IsfineMay 14.2009 — ie6 always sets the selected index of this dynamically created select to 0. So the first option is send to server as selected.[/QUOTE]

Try setting the selectedIndex to -1.
Copy linkTweet thisAlerts:
@sarikanauthorMay 14.2009 — Unfortunately that does not work. It is even worse, if I select all the options, or more than one, even if I've set multiple = true on the select, still only the last one is selected.

In other words, only one opt.selected = true works, and that is the last one executed. If I add the dynamically created select to form, I can select multiple options with the mouse, but it definetely does not work programmatically. I've tried almost everything, and this is a critical piece of functionality that I have to have.

Thanks for your response, and I'll hope someone can help me ?

Regards

Seref
Copy linkTweet thisAlerts:
@JMRKERMay 14.2009 — Can you post a sample of the code that is not working?

Or a link to a sample script?
Copy linkTweet thisAlerts:
@sarikanauthorMay 14.2009 — Ok, there you go, forget about the index == 0 trick, or other index tricks, what matters is this code should result in a multiple selection, but instead it ends up with a list box with the last item selected only

var programmaticSelect = dojo.doc.createElement("select");

programmaticSelect.multiple = true;

programmaticSelect.selectedIndex = -1;

//add unselected options

var unselectedOptions = dndContainer['source'];

for(var uoindex = 0; uoindex <= unselectedOptions.getAllNodes().length; uoindex++){

if (uoindex == 0){

programmaticSelect.options[0] = new Option(null,null);

programmaticSelect.options[0].selected = true;

}else{
programmaticSelect.name = unselectedOptions.getAllNodes()[uoindex - 1].originalId;
programmaticSelect.options[uoindex] = new Option(unselectedOptions.getAllNodes()[uoindex - 1].name, unselectedOptions.getAllNodes()[uoindex - 1].code);
programmaticSelect.options[uoindex].selected = true;
}

}


dojo.byId('mainform').appendChild(programmaticSelect);
Copy linkTweet thisAlerts:
@Sterling_IsfineMay 15.2009 — [CODE]
var programmaticSelect = dojo.doc.createElement("select");
programmaticSelect.multiple = true;
programmaticSelect.selectedIndex = -1;[/CODE]
[/QUOTE]
I meant to do it [I]after[/I] creating the options.
Copy linkTweet thisAlerts:
@sarikanauthorMay 15.2009 — I should have done it like that, but unfortunately it does not change anything.

This code works in a loop, and here is an even funnier thing: if I add an alert just after setting the selected index, the last listbox appears as a dropdown!

Here is a description of my problem: I want to create a listbox programmatically, add options to it and add it to a form, selecting some options/or none of them. I can't do this in ie 6 at the moment.

Kind regards

Seref
Copy linkTweet thisAlerts:
@Sterling_IsfineMay 15.2009 — I should have done it like that, but unfortunately it does not change anything.

This code works in a loop, and here is an even funnier thing: if I add an alert just after setting the selected index, the last listbox appears as a dropdown!
[/quote]
That's because it [I]is[/I] a dropdown and you're giving it time to appear before submitting the form.

Create all the options first, then in a separate loop select the ones that need to be selected and unselect the rest. Probably better not to set the selectedIndex.
×

Success!

Help @sarikan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...