Martin08
Martin08

Reputation: 21460

how to invoke javascript from Internet Explorer DOM

I'm using VBA (Excel) to manipulate a dropdown box in web page loaded in Internet Explorer. The dropdown box has some javascript attached so that the page's UI controls will change when the dropdown selection is changed.

I use the below function to (automatically) select an option from the dropdown. It changes the value of the dropbox but it does not trigger the UI changes (as compared to when I manually change the dropdown box).

Sub selectFromDropdown(ByRef dropdown As HTMLSelectElement, ByVal optionName As String)
    Dim opts
    Dim opt As HTMLOptionElement

    dropdown.Focus
    dropdown.setCapture
    Set opts = dropdown.children
    For i = 1 To opts.Length
        Set opt = opts.Item(i)
        If opt.Text = optionName Then
            opt.Selected = True
            dropdown.selectedIndex = opt.Index
            Exit For
        End If
    Next
    dropdown.releaseCapture
End Sub

So how can I trigger the javascript that is attached to this dropdown box? Or in general, how can I fire an event programmatically via the API provided by MS Internet Explorer? Thank you.

Upvotes: 2

Views: 827

Answers (2)

QHarr
QHarr

Reputation: 84465

You should show enough HTML that we can see the javascript attached and the type of event.

Lets say it is an "onclick"

You could have

opt.Selected = True
opt.FireEvent "onclick"

Upvotes: 1

Shrik
Shrik

Reputation: 154

call your javascript function explicitly when you set combobox value.

Sub selectFromDropdown(ByRef dropdown As HTMLSelectElement, ByVal optionName As String)
    Dim opts
    Dim opt As HTMLOptionElement

    dropdown.Focus
    dropdown.setCapture
    Set opts = dropdown.children
    For i = 1 To opts.Length
        Set opt = opts.Item(i)
        If opt.Text = optionName Then
            opt.Selected = True
            dropdown.selectedIndex = opt.Index
            'Call hear javascript function.
            Exit For
        End If
    Next
    dropdown.releaseCapture
End Sub

Upvotes: 0

Related Questions