Afaan

How do I select an option from a dropdown in IE if it is updated by another select by AJAX?

12 posts in this topic

Is there any way to select an option from a <select> which is itself updated by a previous <select> by AJAX?

Share this post


Link to post
Share on other sites



I would imagine so.

Share this post


Link to post
Share on other sites
Just now, Danp2 said:

I would imagine so.

And that way is?

Share this post


Link to post
Share on other sites

Using _IEFormElementOptionSelect I would imagine. <shrug>

You asked a question and I answered it. If you want assistance with a specific website, then you will need to provide more information, such as:

  • The site name / URL
  • Your code showing what you have tried thus far

If the site isn't open to the public, then it would be good if you found an example of one that is so that we can use it to test and provide you with a working solution.

1 person likes this

Share this post


Link to post
Share on other sites

_IEFormElementOptionSelect works fine with regular <select>s. I use it to set the first select which triggers an ajax request which in turn gets the options for the second select. If I call _IEFormElementOptionSelect on that, it just does nothing. Is there any way that I can wait for the second select to load the options first before attempting to select one?

My code so far:

Func WaitForSelect($argForm, $argSelect, $argOptVal)
    While 1
        $options = _IETagNameAllGetCollection(_IEFormElementGetObjByName($argForm, $argSelect))
        For $opt In $options
            Sleep(100)
            If $opt.value = $argOptVal Then
                _IEFormElementOptionSelect(_IEFormElementGetObjByName($argForm, $argSelect), $argOptVal)
                If _IEFormElementGetObjByName($argForm, $argSelect).value = $argOptVal Then
                    ExitLoop 2
                EndIf
            EndIf
        Next
    WEnd 
EndFunc

It works sometimes, sometimes it doesn't. I need something reliable.

Thanks!

Share this post


Link to post
Share on other sites

You are making multiple calls to _IEFormElementGetObjByName. Is there a reason that you can't call it once and then use the object reference for subsequent calls, ie:

$oSelect = _IEFormElementGetObjByName($argForm, $argSelect)

$options = _IETagNameAllGetCollection($oSelect)

Also, please provide a better description than "doesn't work". What happens? Does it loop forever in the function? Or what?

Share this post


Link to post
Share on other sites

Yeah, I could do that.

And by "doesn't work", I meant that it fails to select the option and just moves on to the next field.

Share this post


Link to post
Share on other sites

I wonder if something like this would work better?

Func WaitForSelect($argForm, $argSelect, $argOptVal, $argDelay = 500)
    Local $oSelect, $oOptions, $nCnt1, $nCnt2
    
    $oSelect = _IEFormElementGetObjByName($argForm, $argSelect)
    
    While 1
        $oOptions = _IETagNameAllGetCollection($oSelect)
        $nCnt1 = @extended
        
        Sleep($argDelay)
        
        $oOptions = _IETagNameAllGetCollection($oSelect)
        $nCnt2 = @extended
        
        If $nCnt1 = $nCnt2 Then
            _IEFormElementOptionSelect($oSelect, $argOptVal)
            ExitLoop
        EndIf
    WEnd 
EndFunc

 

Share this post


Link to post
Share on other sites

I found a work-around which seems to work fine.

Func SetAjaxSelect($argForm, $argSelect, $argOptVal)
    $theSelect = _IEFormElementGetObjByName($argForm, $argSelect)
    $options = 0
    Do
        $options = _IETagNameAllGetCollection($theSelect)
        Sleep(50)
    Until IsObj($options)
    For $opt in $options
        If $opt.value = $argOptVal Then
            Sleep(200)
            _IEFormElementOptionSelect($theSelect, $argOptVal)
            Return
        EndIf
    Next
EndFunc

 

Share this post


Link to post
Share on other sites

Glad you found a working solution. Can you explain why you need the For loop?

Share this post


Link to post
Share on other sites

Since the select is being dynamically updated, I need to make sure that the option I am selecting does indeed exist in the option list. That's why I need the For loop.

Share this post


Link to post
Share on other sites

How do you detect that the option didn't exist in the calling script?

FWIW, I would have tried removing the loop, calling _IEFormElementOptionSelect, and then checking to see if the call succeeded / failed.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now