0

here is my html

  <div class="test">
            <div class="select">
              <select name="slct" id="slct">
                <option class="default_option" value="A">A</option>

                <option value="AAAA">AAAA</option>
                <option value="CNAME">CNAME</option>
                <option value="MX">MX</option>
                <option value="NS">NS</option>
                <option value="PTR">PTR</option>
                <option value="SOA">SOA</option>
                <option value="SRV">SRV</option>
                <option value="TXT">TXT</option>
                <option value="CAA">CAA</option>
              </select>
            </div>
          </div>

I want to trigger an event when dropdown menu is opened.

so, I have written this jquery,

 $('.test').click(function () {
    // $(this).parent().toggleClass("active");
    console.log('hello');
  });

But the problem is, the console is not printed when the dropdown is opened. the console is printed when the dropdown is closed.

2
  • For me your code works well. Commented Jun 6, 2021 at 10:07
  • @s.kuznetsov I mean when I open the dropdown. I need to trigger an event. but in my current implementation, the event triggers when I click on the first option from dropdown. Commented Jun 6, 2021 at 10:24

2 Answers 2

1

I think you are attaching the event before the DOM is fully loaded

You should attach the event inside $(document).ready(function(){...

OR: Use .on() which ensure that the event will be attached to the element that is added to DOM at a later time.

Also, if you want the code to be executed only when open you should use focus:

$('body').on('focus', '.test', function () {

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>
  $('body').on('focus', '.test', function () {
    // $(this).parent().toggleClass("active");
    console.log('hello');
  });
</script>

<div class="test">
  <div class="select">
    <select name="slct" id="slct">
      <option class="default_option" value="A">A</option>

      <option value="AAAA">AAAA</option>
      <option value="CNAME">CNAME</option>
      <option value="MX">MX</option>
      <option value="NS">NS</option>
      <option value="PTR">PTR</option>
      <option value="SOA">SOA</option>
      <option value="SRV">SRV</option>
      <option value="TXT">TXT</option>
      <option value="CAA">CAA</option>
    </select>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

5 Comments

the event is triggered, when I click on the dropdown options. but, what I want is, immediately trigger the event, as soon as the dropdown is open
@Ashik, did you try focus event which I have mentioned in the updated answer?
yep. it is fixed now. what I have currently done is this: gist.github.com/ashiqdev/8e1ebefd1e3cf15c12b06e315bf434bb but the problem is, when I focus again, event is not triggered
@Ashik, focus event works once, to make that work you lose the event by clicking else where and focus again:)
clicking elsewhere means? by writing code, click elsewhere? actually, when a focus event is triggered I apply a class so that the dropdown icon is changed. so, when user again clicks on the dropdown icon I need to apply the same thing.
0

you can use a custom dropdown, not the <select> element

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.