Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add onchange event to ul tag elements?

I have a drop down with set of 'ul' and 'li' like this,

<div style="float:left;">
    <div class="group">
        <div style="width:80px;">Dropdown<i class="fa fa-caret-down"></i></div>
            <ul style="float:left;">
                <li><a href="javascript:void(0);">Option1</a></li>
                <li><a href="javascript:void(0);">Option2</a></li>
                <li><a href="javascript:void(0);">Option3</a></li>
            </ul>
    </div>
</div>

I want to add onchange() event to ul tag. I can say onchange i have to call one function SelectedItem(optionselected) with selected 'li' text as parameter.Anyone Please help me. Thank you.

like image 467
user3136030 Avatar asked Sep 01 '25 03:09

user3136030


1 Answers

The change event is sent to an element when its value changes. This event is limited to elements, <textarea> boxes and <select> elements. For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.

see http://api.jquery.com/change/

So .change() doesn't work on an unordered list...

Use click event for it

    $('li').click(function(){
     //your code
 });
like image 188
Techy Avatar answered Sep 02 '25 17:09

Techy