Results 1 to 3 of 3

Thread: Click event doesn't fire up

  1. #1
    Join Date
    Jan 2017
    Posts
    29
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Click event doesn't fire up

    Hi,

    Looking at this Plunker example:

    slideToggle example

    I want that if I click on one of the select boxes, the next div right after it, slides up. To achieve this, I wrote this code:

    Code:
    // Event Delegation
      $('#container').on('click', '.my-select', function() {
        $(this).next(".my-div").slideToggle();
      });
    It works fine. But I have to click a select box twice to fire up the slideToggle event. Why is that and how can I fix it?

    Thanks
    Last edited by kayut; 02-06-2017 at 11:08 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm not seeing that. What browser? I do notice that you see the option list when you click a select that isn't currently displaying its list of options. Each one has only the one option, but it seems unnecessary and distracting. You can set the options' display none in style. I also notice that you have .focus() in your code on the div. You cannot technically focus on a non-form element. You could blur the select:

    Code:
      // Event Delegation
      $('#container').on('click', '.my-select', function() {
        $(this).blur().next(".my-div").slideToggle();
      });
    But having set the options' display none in style:

    Code:
    .my-select option {display: none;}
    even using blur seems unnecessary:

    Code:
      // Event Delegation
      $('#container').on('click', '.my-select', function() {
        $(this).next(".my-div").slideToggle();
      });
    as the focus now shows the active element (one last clicked).

    Edit: I tried this in Opera (like Chrome), and it was good as above, but in IE the blur seems preferable and the display none on the options unnecessary, but for cross compatibility I would keep both.


    Edit: But in Firefox the display none on the options is bad, can't see the question's text at all, so using just the blur for all would be a decent compromise, but you still get the drop down of the select list in Firefox

    Perhaps using another element instead of a select would be good, like an A tag or a DIV. Form elements often do not behave consistently cross browser.
    Last edited by jscheuer1; 02-06-2017 at 02:22 PM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    kayut (02-06-2017)

  4. #3
    Join Date
    Jan 2017
    Posts
    29
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    @jscheuer1,
    WHAT A GREAT ANSWER!
    Thank you so much!

Similar Threads

  1. adding on click event
    By noobie123 in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 10-09-2014, 01:09 PM
  2. How to fire click event of an LI tag?
    By round in forum JavaScript
    Replies: 12
    Last Post: 01-16-2013, 01:59 PM
  3. [HELP] Click event inside javascript
    By 4rum25 in forum JavaScript
    Replies: 0
    Last Post: 09-14-2010, 07:51 AM
  4. Replies: 1
    Last Post: 04-03-2010, 11:21 PM
  5. how to force fire the onChange event
    By rizlaa in forum JavaScript
    Replies: 5
    Last Post: 03-15-2009, 10:54 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •