aaronk Well-Known Member Licensed User Longtime User Dec 17, 2020 #1 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: Copy <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: Copy 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: Copy 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 ?
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: Copy <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: Copy 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: Copy 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 ?
Erel B4X founder Staff member Licensed User Longtime User Dec 20, 2020 #2 Always use lower case ids. Code: Copy Dim SelectedOption As String = ws.EvalWithResult($"return $('#mylist').find(":selected").text();"$, Null).Value Source: https://stackoverflow.com/questions/10659097/jquery-get-selected-option-from-dropdown Upvote 0
Always use lower case ids. Code: Copy Dim SelectedOption As String = ws.EvalWithResult($"return $('#mylist').find(":selected").text();"$, Null).Value Source: https://stackoverflow.com/questions/10659097/jquery-get-selected-option-from-dropdown
aaronk Well-Known Member Licensed User Longtime User Dec 20, 2020 #3 Erel said: Dim SelectedOption As String = ws.EvalWithResult($"return $('#mylist').find(":selected").text();"$, Null).Value Click to expand... The above is returning the same value as if I use: B4X: Copy 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: Copy Dim SelectedOption As String = ws.EvalWithResult($"return document.getElementById("mylist").options[document.getElementById("mylist").selectedIndex].id"$, Null).Value Upvote 0
Erel said: Dim SelectedOption As String = ws.EvalWithResult($"return $('#mylist').find(":selected").text();"$, Null).Value Click to expand... The above is returning the same value as if I use: B4X: Copy 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: Copy Dim SelectedOption As String = ws.EvalWithResult($"return document.getElementById("mylist").options[document.getElementById("mylist").selectedIndex].id"$, Null).Value