skip to Main Content

Good Morning,
I am new to coding and learning laravel for home project. I am trying to get data from database as per dropdown selection. The code for dropdown item is as below


<div class="dropdown">

            <button class="btn p-0" type="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              <i class="bx bx-dots-vertical-rounded"></i>
            </button>

            <div id="milkstatementdropdown" name="milkstatementdropdown"class="dropdown-menu dropdown-menu- end" aria-labelledby="milkstatementdropdown">
              <li><a class="dropdown-item" href="javascript:void(0);">Yesterday</a></li>
              <li><a class="dropdown-item" href="javascript:void(0);">Last 7 Days</a></li>
              <li><a class="dropdown-item" href="javascript:void(0);">Last Month</a></li>
              <li><a class="dropdown-item" href="javascript:void(0);">Last 3 Month</a></li>
              <li><a class="dropdown-item" href="javascript:void(0);">Last 6 Month</a></li>
            </div>

          </div>

`
After selection of drop down item, need to get value and then pass the same to controller using AJAX.

below is the script for getting the selection value and process ajax.

`

 $("#milkstatementdropdown").change(function(){
                  var duration = $(this).val();
                  alert(course);

                  // ajax call


                });

`

i am not able to get the Alert..

Did i miss something and make mistake in code…

Thanks in Advance.

3

Answers


  1. try this

    $('#id').on('select2:close', function () {
        //do anything
    });
    
    Login or Signup to reply.
  2. Try this one // You can use Anything

    $('#milkstatementdropdown li a').on('click', function(){
          alert($(this).text());
          //do anything
    });
    
    Login or Signup to reply.
  3. You have to use onClick beacuase your dropdown is not an Select Tag:-

    <li><a class="dropdown-item" href="javascript:void(0);" onclick="get_data('Yesterday')">Yesterday</a></li>
    <li><a class="dropdown-item" href="javascript:void(0);"  onclick="get_data('Last 7 Days')">Last 7 Days</a></li>
    <li><a class="dropdown-item" href="javascript:void(0);" onclick="get_data('Last Month')">Last Month</a></li>
    <li><a class="dropdown-item" href="javascript:void(0);" onclick="get_data('Last 3 Month')">Last 3 Month</a></li>
    <li><a class="dropdown-item" href="javascript:void(0);" onclick="get_data('Last 6 Month')">Last 6 Month</a></li>
    

    And then in your Javascript part you have make get_data function to hanle onClick event.

    function get_data(duration){
        // Now you can get data from controller according to received 'duration'
       // ajax call
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search