Amit Kumar
Amit Kumar

Reputation: 417

Get the value of dropdown menu which is clicked in a webpage

I am trying to get the value of dropdown menu which is clicked in a page. Here is what I am doing now :

<c:forEach items="${menuItem.dropdown}" var="dItem">
    <li>
        <a href="${dropdownItem.link}" onclick="<c:set var="clickedDropdown" value="${dItem.name}"/>">${dItem.name}</a>
    </li>
</c:forEach>

But when I clicked on the dropdown menu, it sets the the value of last dropdown menuitem. I am not getting correct value. Is there any way, I can get that dropdown menu item value?

Upvotes: 0

Views: 57

Answers (1)

Minar Mahmud
Minar Mahmud

Reputation: 2665

onclick="<c:set var="clickedDropdown" value="${dItem.name}"/>"

Although <c:set var="clickedDropdown" value="${dItem.name}"/> is inside onclick, it will execute beforehand when the jsp compiles. And as it is inside foreach, naturally the last value remains as the value of the variable.

Even if you want to set the variable using javascript, you cannot, because javascript works in frontend and jstl works in backend.

So, apparently there is actually no way to achieve this. You need to change your approach completely.

Upvotes: 1

Related Questions