B4J Question Websocket - Get HTML Selected Option ID

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am trying to get the id value of a selected item id from a select drop down using my B4J app.

In my HTML I have:
HTML:
<select id="MyList">                                               
    <option id='item1'>this is item 1</option>
    <option id='item2'>this is item 2</option>
    <option id='item3'>this is item 3</option>
</select>

In My B4J project (non-UI) I have a button and when I click on it I have tried to get the selected option id without much luck.

Here is what I have tried..

B4X:
Log(ws.GetElementById("MyList").GetProp("id").Value) ' returns MyList
Log(ws.GetElementById("MyList").GetProp("value").Value) ' returns 'this is item 2'
Log(ws.GetElementById("MyList").GetProp("selectedIndex").Value) ' returns 1 (when the second item is selected)

I can't work out how to get the id of the selected item, such as 'item2'

In JavaScript I can get the value like:
JavaScript:
alert(document.getElementById("MyList").options[document.getElementById("MyList").selectedIndex].id); // returns 'item2'

Any ideas on how to get the selected option ID (for example, item2) of the selected item ?
 

aaronk

Well-Known Member
Licensed User
Longtime User
Dim SelectedOption As String = ws.EvalWithResult($"return $('#mylist').find(":selected").text();"$, Null).Value
The above is returning the same value as if I use:
B4X:
Log(ws.GetElementById("mylist").GetProp("value").Value) ' returns 'this is item 2'

I ended up using the following code and it retuned the option ID of the selected item:
B4X:
Dim SelectedOption As String = ws.EvalWithResult($"return document.getElementById("mylist").options[document.getElementById("mylist").selectedIndex].id"$, Null).Value
 
Upvote 0
Top