my dog learned polymorphism
The moose likes HTML, CSS and JavaScript and the fly likes alert() is changing script function Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "alert() is changing script function" Watch "alert() is changing script function" New topic
Author

alert() is changing script function

K Dombroski
Ranch Hand

Joined: Dec 03, 2005
Posts: 39
I have a javascript function that works perfectly when I have an alert right before the final line, but when I take the alert out, it no longer works. How is this possible, and what can I do to fix it?
Here is what I have:

function setSelection(selectObject, value) {
for (var i = 0; i < selectObject.options.length; i++) {
if (selectObject.options[i].value == value)
{
alert(i);
selectObject.options[i].selected=true
}
}
}

If the alert is there, it selects the appropriate item. However, if I comment out the alert, the select box just selects the first item in the list. Any help is appreciated, as I am completely baffled.
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 788
Try using selectedIndex instead after the loop finishes. Once the correct element is found set selectedIndex to i and abort the loop.
K Dombroski
Ranch Hand

Joined: Dec 03, 2005
Posts: 39
Thanks for your reply! I tried that, but I get the same result: If the alert is not there, it selects the first item in the list, but if the alert is there, it selects the item I tell it to. One thing I did notice: It selects the first item in the list every time, but then after I click "OK" on the alert, it changes the selected item to the one I want it to be.

(In case that doesn't make sense, I'm converting a textbox to a select via ajax, so when the select appears, it selects the first item in the list. If I have the alert there, it then selects the correct item, but if it is not, it seems to just skip it. I had thought it might be a timing issue, but putting in code to make it wait doesn't really seem to work either, unless I am doing it wrong.)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56554
    
  14

Sounds like a race condition to me. The alert is changing the timing of the competing conditions.
[ April 30, 2007: Message edited by: Bear Bibeault ]

[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
K Dombroski
Ranch Hand

Joined: Dec 03, 2005
Posts: 39
Any idea how I can fix it? I tried putting in something to make it wait for a bit, but it ended up delaying the whole switch from textbox to select, rather than waiting in the spot where I put the wait code. My js is pretty rough; any hints would be appreciated. Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56554
    
  14

What code is performing the switch? Why is the setting of the selection not part of that code? You need to be sure that the select element exist before trying to manipulate it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56554
    
  14

Also you can simplify your selection set code to:



for single-selection select elements.
K Dombroski
Ranch Hand

Joined: Dec 03, 2005
Posts: 39
Here is the full set of code:



I tried using this js code I found:



and I put in a call to pausecomp in place of the alert, but that delayed everything for some reason, instead of just waiting a bit right before selecting the item.

In case you need it, here is the html that calls it:


[ April 30, 2007: Message edited by: K Dombroski ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56554
    
  14

Please edit your repsonse using UBB code tags to preserve the formatting of the code. That's just too much code to look at in an unformatted block. Please read this for more information.
K Dombroski
Ranch Hand

Joined: Dec 03, 2005
Posts: 39
I went back and edited my previous post by adding the code tags. Thanks for the tip.
K Dombroski
Ranch Hand

Joined: Dec 03, 2005
Posts: 39
Ok, I think, after much experimentation, that this problem is actually an ajax issue. The list has not finished populating by the time I try to select an item in the list (due to the asynchronicity), so it doesn't go through the loop. I don't quite get why an alert works differently from a call to pausecomp, though. If I use the pausecomp method, everything waits (including displaying the dropdown instead of the text box, even though that code is called several lines earlier and does not use any ajax), but if I use the alert, the ajax call continues with no troubles, and by the time I hit "OK" the list is populated, so I can select something. How can I make it so that it does not try to select anything until after the list is loaded? I am using the DWR toolkit and am very new to ajax.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56554
    
  14

Your code that manipulates the select should be in the same thread that creates it, or at least triggered by that thread. That way, you know that the code won't execute until after the select element is in existence.

If you were using Prototype, you could use one of the onSuccess or onComplete callbacks to perform the selection after the Ajax request is complete. I'm sure that DWR must give you similar capabilities.
K Dombroski
Ranch Hand

Joined: Dec 03, 2005
Posts: 39
Thanks so much! Based on your suggestion, I looked up how I could do that with DWR, and came up with this, which is working like a charm:

Thanks again!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56554
    
  14

Too cool!

The lesson to be learned here is that, once you start adding Ajax into the picture, you can't assume that everything executes in a nice orderly and synchronous manner. You must be very cautious of introducing race conditions in which unrelated threads have inter-dependencies.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: alert() is changing script function
 
Similar Threads
Setting a Parameter in JavaScript
Question listshuttle + suggestionBox
IE/Firefox javascript html list issue
Problem with Javascript string comparison
Help needed with jquery and input text