473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

IE delays in updating DOM SELECT element

Hi

I have an HTML select element in my page and it's multiple property is
disabled (one item at a time mode) but I still want to transfer all the
items in the select to the server when the form is submitted.

I'm using javascript to change the multiple property of the select
element to true and then I'm looping through each option to change its
selected property to true.

Here is the code that's executed just before the form is submitted:

var selectElement = document.getElementById("mySelectElement");
selectElement.multiple = true;
for (var n = 0; n < selectElement.options.length; n++)
{
selectElement.options[n].selected = true;
}

document.forms[0].submit();

PROBLEM....

The browser is too slow in updating the renedered DOM select element on
the page from multiple=false to multiple=true. This causes the looping
code not to work correctly resulting in only the final option being
selected (which is what I'd expect if the select element were in
single-mode). You can verify this is a speed issue by inserting an
alert() between selectElement.multiple = true and the for loop. This
gives the browser enough time to update the page element so that the
every option is selected.

SOLUTION....

Even the slighest delay between the execution of selectElement.multiple
= true and the for loop will allow enough time for the browser to
update the select element, allowing each option to be sucessfully
selected in the loop:

functionA()
{
var selectElement = document.getElementById("mySelectElement");
selectElement.multiple = true;

setTimeout("functionB()", 1);
}

functionB()
{
var selectElement = document.getElementById("mySelectElement");
for (var n = 0; n < selectElement.options.length; n++)
{
selectElement.options[n].selected = true;
}

document.forms[0].submit();
}

Only tested this on IE6.

Anyone else come accross this before? Are there any more elegant
solutions to the problem? Any other thoughts?

Cheers
Rob

May 23 '06 #1
2 2242
Rob Long wrote:
I have an HTML select element in my page and it's multiple property is
disabled (one item at a time mode) but I still want to transfer all
the items in the select to the server when the form is submitted.


Easy solution: Fix your design.
There should be no reason to ever submit unselected elements of a
single-select list. Ever.

If you built the list on the server side, you can built it again when the
user submits. Or you can store it in the session. Etc.

If it's built dynamically on the client side, then you can build up a hidden
field value as you populate the select list, and check its value on the
server side.

Changing a single select to multiple and then selecting every element just
reeks of really bad design.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
May 23 '06 #2
> If it's built dynamically on the client side, then you can build up a hidden
field value as you populate the select list, and check its value on the
server side.
Yeah this is the situation I have - a user can add and remove items
from the list client side.
Changing a single select to multiple and then selecting every element just
reeks of really bad design.


I hear what you're saying, but is it really that much more of a hack
than reading all the elements and populating a hidden CSV field (which
then needs to be parsed server side)? The two approaches are
functionally equivalent, and both centre around sumbitting unselected
options. I can't help but think that if there weren't this problem with
the DOM delay and if the select control had a built-in selectAll()
method then populating a hidden field and having to parse it server
side would then "reek of bad design"?

I.e. if you could do this just before submit

selectElement.multiple = true;
selectElement.selectAll();
form.submit();

instead of

for (var n = 0; n < selectElement.options.length; n++)
{
hiddenField.value += selectElement.options[n].value + ",";
}
form.submit();

I do see that using the select box in this way is not using it "as it
was intended to be used." However the same principle could be applied
to pretty much all web technologies ;-)

Thanks for your input tho!

May 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jason | last post by:
Let's say I have an html form with 20 or 30 fields in it. The form submits the fields via POST to a php page which updates a table in a database with the $_POST vars. Which makes more sense? ...
3
by: Jan Moravec | last post by:
Hi, I have written a Java UDF for DB2 and registered it using the following params: CREATE FUNCTION arsu_bit_value ( varchar(32) for bit data, integer ) RETURNS INTEGER EXTERNAL NAME...
4
by: Dave Taylor | last post by:
I've been using the dataset designer in Visual Studio to create typed datasets for my application by dragging over tables from the Server Explorer and dropping them into the designer. The problem...
13
by: mfreeman | last post by:
The minimal code (VB.NET 2003) needed to show this problem is shown below. All I do is loop through the records in the table and update them without making any changes. Out of 600 records, about...
6
by: recif20002002 | last post by:
Hello I am trying to update table1 and set a new value with a condition selecting only the results for the element with the specified ID and the first one with the Date field null. The query...
6
by: Rich | last post by:
Dim da As New SqlDataAdapter("Select * from tbl1", conn) dim tblx As New DataTable da.Fill(tblx) '--works OK up to this point da.UpdateCommand = New SqlCommand da.UpdateCommand.Connection =...
18
by: barry | last post by:
I'm having a problem creating a delay before each list item gets moved ( http://www.polisource.com/PublicMisc/list-splitter-timeout-bug.html ). Choose any number of columns, vertical layout, and...
10
by: pmarisole | last post by:
This is the first page...... <select size="1" name="Q1_<%=i%>" onChange="calc(<%=i%>)"> <option selected value="<% =rsScores("Q1") <option value="1">1</option> <option value="2">2</option>...
3
by: Evan | last post by:
Hello - My script use a DB file which is written by XML, and the user load this DB file (XML tree in memory), and then do some updating about this tree, such as delete element, generate new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.