User1234
User1234

Reputation: 205

Get value of the selected item from dropdownlist using jsoup

`<div class="col1">

<strong>
<select name="Category" multiple size="4">

<option value="A">A
<option value="B" selected>B
<option value="C">C
<option value="D">D

</select></strong>

</div>`

I have div class containing a drop down list as given above, i just need the value of 'selected' item from the drop down list using Jsoup

Upvotes: 2

Views: 4508

Answers (1)

lefloh
lefloh

Reputation: 10961

Search for the <select> Element, iterate over it’s children and check if the selected attribute is present:

Document doc = Jsoup.parse("your html")
String selectedVal = null;
Elements options = doc.getElementsByAttributeValue("name", "Category").get(0).children();
for (Element option : options) {
    if (option.hasAttr("selected")) {
        selectedVal = option.val();
    }
}

Or in short with a CSS-like selector:

String selectedVal = doc.select("select[name=Category] option[selected]").val();

Upvotes: 5

Related Questions