0

I'm getting desperate about fixing a problem I am facing. With a button I open a dropdown menu. I want to get the value of what I selected (better: the index of what I selected, the "position") when I select something in the onClick function and update my HighChart with the function updateChartDataByCourse(position) with it as a parameter. The function works fine. The page uses jsp. My code is the following:

creating button and dropdownmenu:

        <div class="dropdown" style="display: inline-block; margin-right: 10px">
            <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown"
                    aria-haspopup="true" aria-expanded="true">
                Kurs auswählen:
                <span class="caret"></span>
            </button>
            <ul class="dropdown-menu" aria-labelledby="dropdownMenu" id="course" onclick="enableLFields()">
                <li class="dropdown-header">Kursliste</li>
                <li><c:forEach items="${teachersCourses}" var="course"><a href="#">
                    <option value="${course.id}">${course.name}</option>
                </a></c:forEach></li>
            </ul>
        </div>

and then the onClick-function:

$("#course li a").click(function(){
    $("#dropdownMenu1:first-child").text("Kurs: " + $(this).text());
    $("#dropdownMenu1:first-child").val($(this).text());
    indexOrIdThatShallBePassed = $("#dropdownMenu1").val();
    updateChartDataByCourse(**indexOrIdThatSallBePassed**);

    var dropdown = $("#dropdownMenu1");

console says this index is undefined. I don't get it!

JulesPeace
  • 11
  • 3

1 Answers1

0

To get the value of the clicked item, you could do something like that:

$(this).find('option').attr('value')

Then you would have your indexOrIdThatShallBePassed variable set.

SinDeus
  • 516
  • 1
  • 6
  • 21
  • OH MY GOD that works! I tried for hours to find a solution. Thank you so much! Now there is only one question remaining: Can I somehow use jsp tags <% /%> in an intelligent way to create the – JulesPeace Feb 06 '17 at 14:30
  • I think [this](http://stackoverflow.com/questions/18825950/how-to-get-a-index-value-from-foreach-loop-in-jstl) may answer your last question :) Don't forget to accept my answer if it was helpful! – SinDeus Feb 06 '17 at 14:39
  • You are amazing. Thank you! – JulesPeace Feb 06 '17 at 14:44